C# HorizontalAlign.Center 在 GridView 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14625140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HorizontalAlign.Center not working in GridView
提问by Divz
I am using an ASP Data Grid I am Binding the data field,Header Text dynamically through code behind(c#).
我正在使用 ASP 数据网格我正在通过后面的代码(c#)动态绑定数据字段、标题文本。
I am also setting the style of the column dynamically all are working fine but one of the Column Horizontal-align.Center is not working .
我还在动态设置列的样式,所有这些都工作正常,但其中一个 Column Horizontal-align.Center 不起作用。
I have checked if the style is getting overridden but it is not...
我已经检查过样式是否被覆盖,但它不是......
This the block of code giving an issue:
这是给出问题的代码块:
BoundField field4 = new BoundField();
field4.DataField = dtdata.Tables[0].Columns["data"].ToString();
field4.HeaderText = "Percentage%";
field4.DataFormatString = "{0:N1}%";
field4.SortExpression = "data";
field4.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
grdMarginGrid.Columns.Add(field4);
Can anyone help me in recognising where the issue is..
任何人都可以帮助我识别问题所在..
Thanks in advance, Divya.
提前致谢,迪维亚。
采纳答案by Raghuveer
I think your other styles overriding your new styles.
我认为你的其他风格覆盖了你的新风格。
You can do something like this
你可以做这样的事情
Try adding a CSS class to your gridview from your ASPX code, and assign following styles to your class.
尝试从您的 ASPX 代码向您的 gridview 添加一个 CSS 类,并将以下样式分配给您的类。
<asp:GridView CssClass="grid" runat="server">
<!-- your options -->
</asp:GridView>
.grid td, .grid th{
text-align:center;
}
You can add CSS class from code behind also.. MSDN LINK
您也可以从后面的代码中添加 CSS 类.. MSDN LINK
This will set all your columns text to center in your gridview
这会将您的所有列文本设置为在您的 gridview 中居中
回答by coder
Give ItemStyle-HorizontalAlign="Center" for any field like bound field or Templatefield.
为任何字段(如绑定字段或模板字段)提供 ItemStyle-HorizontalAlign="Center"。
code:
代码:
<asp:TemplateField HeaderText="Something" ItemStyle-HorizontalAlign="Center" >
or
<asp:BoundField DataField="" HeaderText="" ItemStyle-HorizontalAlign="Center">
回答by Ven
The only solution that works for me :
<style>
.HeaderCentered {
text-align: center !important;
}
</style>
in each boundField in gridview declaration add : HeaderStyle CssClass="HeaderCentered" :
在 gridview 声明中的每个 boundField 添加: HeaderStyle CssClass="HeaderCentered" :
<asp:BoundField DataField="idLingua" HeaderText="Lingua" SortExpression="idLingua">
<HeaderStyle CssClass="HeaderCentered" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>