Set ASP.Net GridView Header Text Center aligned using CSS

Last Reply 5 months ago By Andrea

Posted 5 months ago

I have a gridview like below that it hasn't any asp:BoundField in it.

                <asp:GridView ID="GridView1" runat="server" EmptyDataText="هیچ درسی تاکنون تعریف نشده است" BackColor="White" DataKeyNames="Id" BorderColor="#CCCCCC" BorderStyle="None"
                    BorderWidth="1px" CellPadding="3" HorizontalAlign="Center" Width="1068px" Font-Names="Tahoma" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" 
                    HeaderStyle-CssClass="header-center"  HeaderStyle-HorizontalAlign="Center" >
                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
                       <Columns>
                         <asp:CommandField SelectText="انتخاب" ShowSelectButton="True" />
                    </Columns>
                    <FooterStyle BackColor="White" ForeColor="#000066" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"   />
                    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                    <RowStyle ForeColor="#000066" HorizontalAlign="Center" Wrap="True" />
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#007DBB" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#00547E" />
                </asp:GridView>

and my gridview gets the data of header from the below statement:

DataTable dt = new DataTable();
con.Open();
sqlcom.Connection = con;
string q = "select  LessonId As Id ,Description As توضیحات, Status As وضعیت, PracticalHour As 'ساعت عملی', TheoryHour As 'ساعت نظری', PracticalUnit As 'واحد عملی'" +
           ", TheoryUnit As 'واحد نظری', LessonTittle As 'عنوان درس'    from  View_SubjectStudy_Lesson where SubjectStudyId ='" + IDsubjectstudy + "'";
SqlDataAdapter da = new SqlDataAdapter(q, con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
sqlcom.Connection.Close();

I know if I had an asp:BoundField I could use

HeaderStyle HorizontalAlign="Center" 

or I could use this:

.GridHeader
{
    text-align:center !important;   
}
<asp:GridView ID="GridView1" runat="server" HeaderStyle-CssClass="GridHeader">

now, how can I align center the header of gridview?

Posted 5 months ago
Hi @dorsa,
Please try the following

How to set the Gridview Header Text Center Aligned

It might help you.

Cheers Andrea.