Merge Header or header and subheader in gridview

 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Build custom header.
            GridView oGridView = (GridView)sender;
            GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell oTableCell = new TableCell();

            //Add Department
            oTableCell.Text = "";
            oTableCell.ColumnSpan = 4;
        
            oTableCell.Style.Add("background-color", "#E0F0FD");
            oTableCell.Style.Add("border", "1px solid #8BB7D6");
            oTableCell.Style.Add("font-weight", "bold");
            oTableCell.Style.Add("font-family", "Arial,Helvetica,sans-serif");
            oTableCell.Style.Add("color", "#1077C4");
            oGridViewRow.Cells.Add(oTableCell);

            //Add Emp
            oTableCell = new TableCell();
            oTableCell.Text = "Emp";        
            oTableCell.Style.Add("background-color", "#E0F0FD");
            oTableCell.Style.Add("border", "1px solid #8BB7D6");
            oTableCell.Style.Add("font-weight", "bold");
            oTableCell.Style.Add("font-family", "Arial,Helvetica,sans-serif");          
            oTableCell.Style.Add("color", "#1077C4");
            oTableCell.HorizontalAlign = HorizontalAlign.Center;          
            oTableCell.ColumnSpan = 3;
            oGridViewRow.Cells.Add(oTableCell);

            //Add Salary
            oTableCell = new TableCell();
            oTableCell.Text = "Salary";
            oTableCell.Style.Add("background-color", "#E0F0FD");
            oTableCell.Style.Add("border", "1px solid #8BB7D6");
            oTableCell.Style.Add("font-weight", "bold");
            oTableCell.Style.Add("font-family", "Arial,Helvetica,sans-serif");
            oTableCell.Style.Add("color", "#1077C4");
            oTableCell.HorizontalAlign = HorizontalAlign.Center;         
            oTableCell.ColumnSpan = 3;
            oGridViewRow.Cells.Add(oTableCell);

            oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);
        }
    }

Comments