Home
Products
Forums
Help
Publish Article
Go Freelance

Format DataGrid Header at run time. Format GridView Header

Downloads

If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to become a Winista member and download the control for free.

This article will touch upon the topic of that can answer some of the questions related to formatting of datagrid or gridview header. Some of the most commonly asked questions are as follows.

  • How to format GridView DataGrid Header at design time?
  • How to format GridView DataGrid Header at run time?
  • How to change header text of DataGrid or GridView at run time?

If you want to format headers at design time and this formatting is not going to change ever, then the bext place to do would be on ASPX page itself. To set a formatting for all headers you can set HeaderStyle attributes as show in the following snippet.

<asp:GridView ID="ctlGridView" runat="server" OnRowCreated="OnRowCreated" cellpadding="5">
    <headerstyle font-bold=True />
</asp:GridView>							
							

If you want to set style for individual columns then you can set HeaderStyle attributes of individual columns as shown in following code snippet.

<asp:GridView ID="ctlGridView" runat="server" OnRowCreated="OnRowCreated" cellpadding="5">
    <columns>
        <asp:boundfield datafield="Name" headerstyle-forecolor=Blue />
    </columns>
</asp:GridView>							
							

If you are looking into formatting the header of columns at run time then you pretty much follow the techniques that I have discussed in previous articles about formatting row. Only difference is that for formatting header you will be checking for DataControlRowType property to be of type Header. The following code snippet shows how in RowCreated event handler of GridView, all horizontal alignment and colors of headers have been modified along with modification of each header text.

protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        Int32 idx = 0;
        foreach(TableCell cell in e.Row.Cells)
        {
            cell.Text = String.Format("{0}_{1}", cell.Text, (++idx));
            if (idx%2 == 0)
            {
                cell.HorizontalAlign = HorizontalAlign.Right;
            }
            else
            {
                cell.BackColor = System.Drawing.Color.Blue;
                cell.ForeColor = System.Drawing.Color.White;
            }
        }
    }
}							
							
Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix