Wednesday, November 26, 2008

Merging GridView Headers to have multiple Headers in GridView



Here is the example to Have multiple headers in a gridView or merging headers in GridView using C# and ASP.NET

For this you need to create GridView header row in RowCreated Event


<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [DepartMentID],
[DepartMent], [Name], [Location] FROM [Employee]">
</asp:SqlDataSource>
&nbsp;</div>
</form>
</body>
</html>

Now In Code behind, in RowCreated Event of grid view i m creating a new gridview row of header type and than in this row i m adding 2 cells
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
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 = "Department";
HeaderCell.ColumnSpan = 2;
HeaderGridRow.Cells.Add(HeaderCell);

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

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

}
}
}


Download the Sample Code



Other GridView articles:
1. Ajax autocomplete extender textbox in EditItemTemaplate of GridView

2. ASP .NET -Populating dropdown based on the selection of first drop down in DetailsView using FindControl and ItemTemplate

3. Pouplating Multiple DetailsView based on single GridView using DataKeyNames in ASP.NET

4. Search within records in GridView with searchbox in footer and highlight results using AJAX and C# ASP.NET

5. Export GridView from aspx page to Pdf using iTextSharp in asp.net
Add to diigo
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks Add to Mixx! Mixx it! add to del.icio.us saved by 0 users
Subscribe to Feeds

1 comments:

111 said...

This post has been removed by a blog administrator.


.NET Resources

Further Readings

Find More Articles


Followers

Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

Follow me on Twitter

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN