Merge Merger header columns in gridview?
How can I create below type of gridview with merge merged header columns? If anybody have example share 开发者_C百科with me.
Thanks in advance.
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.
精彩评论