In this article I am going to explain How to merge Gridview header cells with C#
As I explained in previous articles bind data with Gridview, If you are new to this article series, please visit How to bind data with Gridview
After bind data Gridview headings shows as below
| # | Name | Code |
I am going to inset an another row above to this headings as below
| Cheque | Bank |
----------------------------------------------
| # | Name | Code |
Cheque header column is one cell and Bank header as with 2 cells merged.
Use the below mentioned code inside the RoeDataBound event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (gvr.RowType == DataControlRowType.Header)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = 1;
cell.HorizontalAlign = HorizontalAlign.Center;
cell.Text = "Cheque";
row.Cells.Add(cell);
cell = new TableCell();
cell.ColumnSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
cell.Text = "Bank";
row.Cells.Add(cell);
GridView1.Controls[0].Controls.AddAt(0, row);
}
}
Please watch video for more details.