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.
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
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With