Skip to main content
Email Password   helpLost your password?

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.
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionPosition of the new header Pin
Agamotto
1:58 22 May '09  
AnswerRe: Position of the new header Pin
Agamotto
2:10 22 May '09  
JokeSukriya Pin
grawas
7:11 14 Apr '09  
GeneralThanks but i have a problem Pin
Philippe43
4:57 28 Jan '09  
GeneralTHANKS A LOT !!! Pin
yonatankr
3:40 11 Nov '08  
GeneralThanks Pin
Sai U
1:08 9 Sep '08  
GeneralThanks Pin
wvl32
12:47 18 Jun '08  
GeneralThanks Pin
cutespice
6:14 6 Jun '08  
GeneralWell... Thanks also! Pin
ronchese
12:08 17 Mar '08  
GeneralBottom pager do not fired event when in sert new row to header Pin
thanasak.polrak
0:03 27 Feb '08  
GeneralRe: Bottom pager do not fired event when in sert new row to header Pin
Thiagarajan Rajendran
7:30 19 Mar '08  
GeneralRe: Bottom pager do not fired event when in sert new row to header Pin
thanasak.polrak
16: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) Pin
dannykb111
13:25 21 Feb '08  
GeneralOnRowCreated Pin
K321
21:50 26 Jan '08  
GeneralSorting.... Pin
amit_ksp
2:43 3 Aug '07  
GeneralRe: Sorting.... Pin
Thiagarajan Rajendran
10:18 8 Aug '07  
GeneralRe: Sorting.... Pin
amit_ksp
12:05 8 Aug '07  
QuestionGreat Code but ... Pin
anikon45
23:25 1 Aug '07  
AnswerRe: Great Code but ... Pin
Thiagarajan Rajendran
10:20 8 Aug '07  
GeneralRe: Great Code but ... Pin
anikon45
23:14 12 Aug '07  
GeneralRe: Great Code but ... Pin
Noman Aftab
1:49 28 Aug '08  
Generalsmall fix Pin
Prashant Atal
0:56 14 Jun '07  
Generalthanks Pin
ceMental
11:41 5 Jun '07  
GeneralHeader text alignment Pin
milan_np
22:55 14 May '07  
GeneralRe: Header text alignment Pin
milan_np
23:07 14 May '07  


Last Updated 30 May 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009