How can create multi header gridview.
How can create multi header gridview.
Here is our code for creating multi Header Gridview .
In this article I am using a gridvew in .aspx .i am using gridview event OnRowCreated
For this.
.aspx part
<asp:GridView ID="GR" runat="server" AutoGenerateColumns="False" OnRowCreated="grvMergeHeader_RowCreated">
<Columns>
<asp:BoundField DataField="name"
HeaderText="name"
/>
<asp:BoundField DataField="age"
HeaderText="age"
/>
<asp:BoundField DataField="salary"
HeaderText="salary"
/>
<asp:BoundField DataField="country"
HeaderText="country"
/>
<asp:BoundField DataField="city"
HeaderText="city"
/>
</Columns>
</asp:GridView>
.CS Part
//Bind gridview at page load.
protected void Page_Load(object sender, EventArgs
e)
{
string qu = "select
* from gridview";
SqlDataAdapter ad = new
SqlDataAdapter(qu,con);
DataTable dt = new
DataTable();
ad.Fill(dt);
GR.DataSource = dt;
GR.DataBind();
}
//Row_created for creating its headers.
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);
TableCell HeaderCell = new
TableCell();
HeaderCell.Text = "Header 1";
HeaderCell.ColumnSpan = 2;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Header2";
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
GR.Controls[0].Controls.AddAt(0, HeaderGridRow);
}
}
Comments
Post a Comment