5,427,303 members and growing! (14,885 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Merge Header - Gridview/DataGrid

By Thiagarajan Rajendran

This article shows how we can merge multiple column into single column in gridview using asp.net 2.0
C# 2.0, C#, Windows, .NET, .NET 2.0, ASP.NET, VS2005, Visual Studio, Dev

Posted: 20 Oct 2006
Updated: 30 May 2008
Views: 44,438
Bookmarked: 34 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 4.54 Rating: 3.55 out of 5
3 votes, 15.8%
1
2 votes, 10.5%
2
0 votes, 0.0%
3
2 votes, 10.5%
4
12 votes, 63.2%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Sample Image - Merge_Header.jpg

Introduction

During development with GridView, we might come across many situations in which we need to extend GridView for our requirements. For example, we need to have a separate header other than the header provided by GridView. In that case we need to add new GridView item of header type. In this article we will see how merge two or more columns.

Requirement

In this example we have simple GridView which is fetching data from xml file and displaying that in the table structure using GridView. In this GridView, we need to add two additional Header with text "Department" and "Employee". First department column should occupy first two columns and Employee column should occupy rest three columns.

Problem

I found in the internet that there are many ways to do this, but we might end up with column alignment problems.

Solution

Here we are going to see one of the best method to do that. To have additional header, we need to add one GridViewRow of header type to GridView inside OnRowCreated event method. The code snippet for doing this,

protected void GridView_Merge_Header_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    { 
        //Build custom header.
        GridView oGridView = (GridView)sender;
        GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
        TableCell oTableCell = new TableCell();
        
                //Add Department
                oTableCell.Text = "Department";
                oTableCell.ColumnSpan = 2;
                oGridViewRow.Cells.Add(oTableCell);

                //Add Employee
                oTableCell = new TableCell();
                oTableCell.Text = "Employee";
                oTableCell.ColumnSpan = 3;
                oGridViewRow.Cells.Add(oTableCell);
                oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);
    }
}   

Below Code for DataGrid (VS.Net 1.0/1.1):

Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
    If e.Item.ItemType = ListItemType.Header Then
        Dim dgItem As DataGridItem
        Dim dgCell As TableCell
        dgItem = New DataGridItem(0, 0, ListItemType.Header)
        dgCell = New TableCell()
        dgCell.ColumnSpan = 2
        dgItem.Cells.Add(dgCell)
        dgCell.Text = "List of Products"
        DataGrid1.Controls(0).Controls.AddAt(0, dgItem)
    End If
End Sub   
That's it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Thiagarajan Rajendran



Occupation: Software Developer (Senior)
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 36 (Total in Forum: 36) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralThanksmemberwvl3212:47 18 Jun '08  
GeneralThanksmembercutespice6:14 6 Jun '08  
GeneralWell... Thanks also!memberronchese12:08 17 Mar '08  
GeneralBottom pager do not fired event when in sert new row to headermemberthanasak.polrak0:03 27 Feb '08  
GeneralRe: Bottom pager do not fired event when in sert new row to headermemberThiagarajan Rajendran7:30 19 Mar '08  
GeneralRe: Bottom pager do not fired event when in sert new row to headermemberthanasak.polrak16:30 19 Mar '08  
QuestionGreat Article -- is it possible to simply replace the Header row with text of your choosing?? (ie Dynamically Create the Header Row)memberdannykb11113:25 21 Feb '08  
GeneralOnRowCreatedmemberK32121:50 26 Jan '08  
GeneralSorting....memberamit_ksp2:43 3 Aug '07  
GeneralRe: Sorting....memberThiagarajan Rajendran10:18 8 Aug '07  
GeneralRe: Sorting....memberamit_ksp12:05 8 Aug '07  
QuestionGreat Code but ...memberanikon4523:25 1 Aug '07  
AnswerRe: Great Code but ...memberThiagarajan Rajendran10:20 8 Aug '07  
GeneralRe: Great Code but ...memberanikon4523:14 12 Aug '07  
GeneralRe: Great Code but ...memberNoman Aftab1:49 28 Aug '08  
Generalsmall fixmemberPrashant Atal0:56 14 Jun '07  
GeneralthanksmemberceMental11:41 5 Jun '07  
GeneralHeader text alignmentmembermilan_np22:55 14 May '07  
GeneralRe: Header text alignmentmembermilan_np23:07 14 May '07  
GeneralJhala Jhalamemberroseline74:43 4 Apr '07  
GeneralWhy does this cast not valid?memberroseline73:39 4 Apr '07  
GeneralRe: Why does this cast not valid?memberThiagarajan Rajendran4:43 4 Apr '07  
GeneralMerge Header - Gridview/DataGridmemberwkrupinsky8:45 26 Feb '07  
AnswerRe: Merge Header - Gridview/DataGridmemberThiagarajan Rajendran9:30 26 Feb '07  
GeneralRe: Merge Header - Gridview/DataGridmemberSumanRoy22:52 23 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 May 2008
Editor:
Copyright 2006 by Thiagarajan Rajendran
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project