HorizontalAlign.Center not working in GridView

I am using ASP Data Grid I am binding a data field, the title of the text dynamically through the code behind (C #).

I also dynamically adjust the column style, everything works fine, but one of the Horizontal-align.Center columns does not work.

I checked if the style is canceled, but that is not ...

This block of code gives the problem:

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 someone help me in recognizing where the problem is.

Thanks in advance, Divya.

+6
source share
3 answers

I think your other styles override your new styles.

You can do something like this

Try adding a CSS class to gridview from your ASPX code and assign the following styles to your class.

  <asp:GridView CssClass="grid" runat="server"> <!-- your options --> </asp:GridView> .grid td, .grid th{ text-align:center; } 

You can also add a CSS class from the code. MSDN LINK

This will cause all column text to be centered in gridview

+15
source

Give ItemStyle-HorizontalAlign = "Center" for any field, such as an associated field or Templatefield.

code:

 <asp:TemplateField HeaderText="Something" ItemStyle-HorizontalAlign="Center" > or <asp:BoundField DataField="" HeaderText="" ItemStyle-HorizontalAlign="Center"> 
+4
source
 The only solution that works for me : <style> .HeaderCentered { text-align: center !important; } </style> 

in each boundField in the gridview declaration add: HeaderStyle Cssclass = "HeaderCentered":

 <asp:BoundField DataField="idLingua" HeaderText="Lingua" SortExpression="idLingua"> <HeaderStyle CssClass="HeaderCentered" /> <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> 
0
source

All Articles