GridView MergeHeaders in asp.net 4.0 using C# .net


There may be some cases when the Gridview Headers has to be displayed with Merged Headers. Mainly used in cases of displaying the details in a Report Format. From this article, I will explain how to merge two columns into single column with a single header aligned horizontally and vertically to the center. In Asp.net using C#.net 4.0.

How to merge two columns into single column


GridView MergeHeaders - Overview


I will explain how to merge two columns into single column with a single header aligned horizontally and vertically to the center. In Asp.net using C#.net 4.0.

Step 1:

1.Create a new project name: GridViewExample

In GridViewExample.aspx
Place sqldatasource


<asp:sqldatasource ID="SqlDataSource1" runat="server" ConnectionString=
"<%$ ConnectionStrings: NorthWind %>"
SelectCommand="select id,name,addressline1,PhoneNo1 from provider">
</asp:sqldatasource>



step 2. Place gridview



<asp:GridView ID="gridviewMergeHeaders" runat="server" AutoGenerateColumns="False"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderStyle="None"
CellPadding="3" CellSpacing="2" ForeColor="Black" GridLines="None"
DataSourceID="SqlDataSource1"
onrowcreated="gridviewMergeHeaders_RowCreated"
onrowdatabound="gridviewMergeHeaders_RowDataBound">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns<
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Addressline1" HeaderText="Address" />
<asp:BoundField DataField="PhoneNo1" HeaderText="PhoneNo" />
</Columns>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

</asp:GridView>



step 3: in web.config write the connection as per your database details


<connectionStrings>
name="NorthWind"
connectionString=
"Data Source=datasourcename;userid=username;password=password;database=databaseName"/<
</connectionStrings>



step 4: in GridViewExample.cs, write the code



protected void gridviewMergeHeaders_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.ColumnSpan = 1;
HeaderCell.RowSpan = 2;
HeaderCell.Text = "ID";
HeaderCell.VerticalAlign = VerticalAlign.Middle;
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderGridRow.Cells.Add(HeaderCell);

HeaderCell = new TableCell();


HeaderCell.ColumnSpan = 1;
HeaderCell.RowSpan = 2;
HeaderCell.Text = "Name";
HeaderCell.VerticalAlign = VerticalAlign.Middle;
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderGridRow.Cells.Add(HeaderCell);

HeaderCell = new TableCell();
HeaderCell.Text = "Location";
HeaderCell.ColumnSpan = 2;
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderGridRow.Cells.Add(HeaderCell);

gridviewMergeHeaders.Controls[0].Controls.AddAt(0, HeaderGridRow);

}


}




output1



To over come this multiple headers, we write a code in RowDataBound and hide these cells.



protected void gridviewMergeHeaders_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
e.Row.Cells[1].Visible = false;
}

}



the final output

output2


Comments

Author: Vijayalakshmi G M03 Dec 2011 Member Level: Gold   Points : 0

Hi,

Good and useful article.

Keep writing these type of articles.

Guest Author: Isaac Mihaeli13 Jul 2012

I searched all over and got some sample code, but it did not work. Once I came across this article and implemented the code for my needs everything worked great.

Very helpful and keep on writing great articles,



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: