1

How can I create below type of gridview with merge merged header columns? If anybody have example share with me.

alt text

Thanks in advance.

James123
  • 11,184
  • 66
  • 189
  • 343

1 Answers1

3

Code for GridView

<asp:GridView ID="grvMergeHeader" runat="server"
              BackColor="LightGoldenrodYellow"
              BorderColor="Tan" BorderWidth="5px"
              CellPadding="3" ForeColor="Black"
              GridLines="None" BorderStyle="None"
              CellSpacing="2"
              AutoGenerateColumns="False"
              DataSourceID="SqlDataSource1"
              OnRowCreated="grvMergeHeader_RowCreated">
    <FooterStyle BackColor="Tan" />
    <SelectedRowStyle BackColor="DarkSlateBlue"
                      ForeColor="GhostWhite" />
    <PagerStyle BackColor="PaleGoldenrod"
                ForeColor="DarkSlateBlue"
                HorizontalAlign="Center" />
    <HeaderStyle BackColor="Tan" Font-Bold="True" />
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
    <Columns>
        <asp:BoundField DataField="DepartMentID"
                        HeaderText="DepartMentID"
                        SortExpression="DepartMentID" />
        <asp:BoundField DataField="DepartMent"
                        HeaderText="DepartMent"
                        SortExpression="DepartMent" />
        <asp:BoundField DataField="Name"
                        HeaderText="Name"
                        SortExpression="Name" />
        <asp:BoundField DataField="Location"
                        HeaderText="Location"
                        SortExpression="Location" />
    </Columns>
</asp:GridView>

CS Code

//row created
protected void grvMergeHeader_RowCreated(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        GridView HeaderGrid = (GridView)sender;
        GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, 
                                                    DataControlRowState.Insert);  //creating new Header Type 
        TableCell HeaderCell = new TableCell(); //creating HeaderCell
        HeaderCell.Text = "Department";
        HeaderCell.ColumnSpan = 2;
        HeaderGridRow.Cells.Add(HeaderCell);//Adding HeaderCell to header.

        HeaderCell = new TableCell();
        HeaderCell.Text = "Employee";
        HeaderCell.ColumnSpan = 2;
        HeaderGridRow.Cells.Add(HeaderCell);

        grvMergeHeader.Controls[0].Controls.AddAt(0, HeaderGridRow);
    }
}

I hope this will work for you.

gevorg
  • 4,835
  • 4
  • 35
  • 52
kbvishnu
  • 14,760
  • 19
  • 71
  • 101