Click here to Skip to main content
Licence CPOL
First Posted 20 Oct 2006
Views 116,270
Downloads 899
Bookmarked 60 times

Merge Header - GridView/DataGrid

By | 30 May 2008 | Article
This article shows how we can merge multiple columns into a single column in a GridView using ASP.NET 2.0.

Sample Image - Merge_Header.jpg

Introduction

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

Requirement

In this example, we have a simple GridView which fetches data from an XML file and displays that in the table structure. In this GridView, we need to add two additional headers with text "Department" and "Employee". The department column should occupy the first two columns and the Employee column should occupy the rest of the three columns.

Problem

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

Solution

Here, we are going to see one of the best methods to do this. To have an additional header, we need to add a GridViewRow of type Header to the GridView inside the OnRowCreated event. Here is 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);
    }
}

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

Software Developer (Senior)

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRe: Sorting.... PinmemberThiagarajan Rajendran9:18 8 Aug '07  
GeneralRe: Sorting.... Pinmemberamit_ksp11:05 8 Aug '07  
QuestionGreat Code but ... Pinmemberanikon4522:25 1 Aug '07  
AnswerRe: Great Code but ... PinmemberThiagarajan Rajendran9:20 8 Aug '07  
GeneralRe: Great Code but ... Pinmemberanikon4522:14 12 Aug '07  
GeneralRe: Great Code but ... PinmemberNoman Aftab0:49 28 Aug '08  
Generalsmall fix PinmemberPrashant Atal23:56 13 Jun '07  
Your solution works fine.
 
The only change would really help if you have the pager activated for the top and bottom.
 
oGridView.Controls(0).Controls.AddAt(1, oGridViewRow)
 
Just changing this line would help and the header will not then float over the top of the Pager Smile | :) .
Generalthanks PinmemberceMental10:41 5 Jun '07  
GeneralHeader text alignment Pinmembermilan_np21:55 14 May '07  
GeneralRe: Header text alignment Pinmembermilan_np22:07 14 May '07  
GeneralJhala Jhala Pinmemberroseline73:43 4 Apr '07  
QuestionWhy does this cast not valid? Pinmemberroseline72:39 4 Apr '07  
AnswerRe: Why does this cast not valid? PinmemberThiagarajan Rajendran3:43 4 Apr '07  
GeneralMerge Header - Gridview/DataGrid Pinmemberwkrupinsky7:45 26 Feb '07  
AnswerRe: Merge Header - Gridview/DataGrid PinmemberThiagarajan Rajendran8:30 26 Feb '07  
GeneralRe: Merge Header - Gridview/DataGrid PinmemberSumanRoy21:52 23 Apr '07  
GeneralRe: Merge Header - Gridview/DataGrid Pinmemberhhf648:21 15 Aug '07  
GeneralThanks! IOU1 Pinmemberwebbuilder7:21 16 Jan '07  
GeneralUserControl On the created cells Pinmemberalam_pune7:38 12 Jan '07  
QuestionDataGridView? PinmemberJason Law19:42 10 Jan '07  
AnswerRe: DataGridView? PinmemberThiagarajan Rajendran7:49 16 Jan '07  
GeneralThanks!!! PinmemberVikramSinghGehlot7:37 29 Nov '06  
GeneralProblem with updating Pinmemberzarko.petrovic0:59 20 Nov '06  
GeneralRe: Problem with updating PinmemberThiagarajan Rajendran4:12 20 Nov '06  
GeneralRe: Problem with updating Pinmemberzarko.petrovic20:10 20 Nov '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 30 May 2008
Article Copyright 2006 by Thiagarajan Rajendran
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid