How to create a Main Header in Gridview along with the GridView Column Headers?

In this post, I will show you how to add a header in GridView.

You have to manually add a row to the table generated by the GridView follows.

<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="10"
OnPreRender="GridView1_PreRender"/>

protected void grd_PreRender(object sender, EventArgs e)
{

GridViewRow gv = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell tbCell = new TableCell();
tbCell.ColumnSpan = 3;
tbCell.Text = "GridView Header";
tbCell.Attributes.Add("style", "text-align:center");
gv.Cells.Add(tbCell);
GridView1.Controls[0].Controls.AddAt(0, gv);

}
Reactions

Post a Comment

0 Comments

Close Menu