Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HorizontalAlign.Center not working in GridView

Tags:

c#

.net

asp.net

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.

like image 526
Divz Avatar asked Jan 31 '13 11:01

Divz


2 Answers

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

like image 84
Raghuveer Avatar answered Sep 30 '22 03:09

Raghuveer


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">
like image 26
coder Avatar answered Sep 30 '22 03:09

coder