Reputation: 105
I am using an ASP Data Grid I am Binding the data field,Header Text dynamically through code behind(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 .
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.
Upvotes: 8
Views: 32301
Reputation: 86
The only solution that works for me :
<style>
.HeaderCentered {
text-align: center !important;
}
</style>
in each boundField in gridview declaration add : HeaderStyle CssClass="HeaderCentered" :
<asp:BoundField DataField="idLingua" HeaderText="Lingua" SortExpression="idLingua">
<HeaderStyle CssClass="HeaderCentered" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
Upvotes: -1
Reputation: 2636
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.
<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
This will set all your columns text to center in your gridview
Upvotes: 16
Reputation: 2000
Give ItemStyle-HorizontalAlign="Center" for any field like bound field or Templatefield.
code:
<asp:TemplateField HeaderText="Something" ItemStyle-HorizontalAlign="Center" >
or
<asp:BoundField DataField="" HeaderText="" ItemStyle-HorizontalAlign="Center">
Upvotes: 6