Click here to Skip to main content
15,867,594 members
Articles / Web Development / ASP.NET
Article

Merge DataGrid Header

Rate me:
Please Sign up or sign in to vote.
4.62/5 (44 votes)
26 Oct 2004CPOL1 min read 390.1K   3.8K   85   63
Shows how to merge DataGrid header at run-time.

Sample Image

Sample Image

Introduction

This article describes the technique to merge the header of a DataGrid by redirecting the Render method of the DataGrid items.

Background

I found many times the need to merge the header of a DataGrid. So, when I was having spare time, I tried to find a way to do it, and here it is. I know that if you need to merge headers, you can use the Repeater control instead. But if you are fond of DataGrid (just like me), or may be you already used DataGrid, then this article is for you.

Using the code

When rendered, a DataGrid will be converted into a HTML Table element and the header will be the first HTML TR element. So, to have a merged header, we need to have control in the rendering of the header. This can be achieved by redirecting the Render method of the DataGrid header using the SetRenderMethodDelegate of the DataGrid header on ItemCreated event, like this:

C#
private void Datagrid1_ItemCreated(object sender, 
          System.Web.UI.WebControls.DataGridItemEventArgs e) 
{ 
    //*** Examine if the item created is the header item 
    ListItemType lit = e.Item.ItemType; 
    if(ListItemType.Header == lit) 
    { 
        //*** Redirect the default header rendering method to our own method 
        e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod)); 
    } 
}

And here is our own Render method:

C#
/// <summary> 
/// This is our custom render method for the grid header item 
/// </summary> 
/// <param name="writer"></param> 
/// <param name="ctl"></param> 
private void NewRenderMethod(HtmlTextWriter writer, Control ctl) 
{ 
    //***  We don't need to write the <TR> tag
    //     because it's already written by the writer 
    //     so now we write the Name column 
    writer.Write("<TD colspan=\"3\" align=\"center\">Name</TD>\n"); 

    //***  The Age column must have the rowspan attribute
    //     and must be rendered inside the 
    //     first row so it will centered vertically 
    TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count-1]; 
    cell.Attributes.Add("rowspan","2"); 
    cell.RenderControl(writer); 

    //***     Now we close the first row, so we can insert the second one 
    writer.Write("</TR>\n"); 

    //***  Add the style attributes that was defined in design time 
    //     to our second row so they both will have the same appearance 
    DataGrid1.HeaderStyle.AddAttributesToRender(writer); 

    //***     Insert the second row 
    writer.RenderBeginTag("TR"); 

    //***  Render all the cells that was defined
    //     in design time, except the last one 
    //     because we already rendered it above 
    for(int i=0; i<= ctl.Controls.Count-2; i++) 
    { 
        ctl.Controls[i].RenderControl(writer); 
    } 

    //***  We don't need to write the </TR> close tag
    //     because the writer will do that for us 
    //     and so we're done :) 
}

I have created a decorator class to decorate a DataGrid (ASPNetDatagridDecorator class) to have a merge header, and all you need to do is define the header cell like this (you can use the auto format feature, but doesn't work for all):

C#
private void Page_Load(object sender, System.EventArgs e)
{
    // Put user code to initialize the page here
    if(!this.IsPostBack)
    {
        TableCell cell = null;
        DataGrid1.DataSource = GetData();
        DataGrid1.DataBind(); 

        m_add.DatagridToDecorate = Datagrid2;
        ArrayList header = new ArrayList();

        // cell = new TableCell();
        // cell.Text = "Code";
        // cell.RowSpan = 2;
        // cell.HorizontalAlign = HorizontalAlign.Center;
        // header.Add(cell);

        cell = new TableCell();
        cell.Text = "Name";
        cell.RowSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "Name";
        cell.ColumnSpan = 3;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "Age";
        cell.RowSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "School";
        cell.ColumnSpan = 3;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "Religion";
        cell.RowSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        m_add.AddMergeHeader(header);

        Datagrid2.DataSource = GetData();
        Datagrid2.DataBind();

    }

}

License

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


Written By
Web Developer
Indonesia Indonesia
Irwansyah is a web developer currently using ASP.Net. Irwansyah main interests lie in developing business application framework.

Irwansyah intends to work overseas one day and explore the world till the end of the world.

Comments and Discussions

 
GeneralDropdownList in Merged Header Pin
Dick Clark9-Dec-05 0:39
Dick Clark9-Dec-05 0:39 
GeneralDropDownList in header Pin
Dick Clark9-Dec-05 0:38
Dick Clark9-Dec-05 0:38 
QuestionOverlapping titles Pin
revie24-Nov-05 21:19
revie24-Nov-05 21:19 
GeneralGood Article Pin
FrancisFoo1-Sep-05 20:11
FrancisFoo1-Sep-05 20:11 
GeneralExcellent details Pin
Member 5628126-Jul-05 6:22
Member 5628126-Jul-05 6:22 
Questioncan't sort on the added cell? Pin
margiex5-Jul-05 0:16
margiex5-Jul-05 0:16 
GeneralVB.NET Pin
eirikr26-May-05 12:10
eirikr26-May-05 12:10 
GeneralRe: VB.NET Pin
Christian Graus26-May-05 12:40
protectorChristian Graus26-May-05 12:40 
GeneralRe: VB.NET Pin
Cliff_k1017-Apr-06 13:29
Cliff_k1017-Apr-06 13:29 
GeneralRe: VB.NET Pin
Christian Graus9-Apr-06 11:50
protectorChristian Graus9-Apr-06 11:50 
GeneralRe: VB.NET Pin
Lee hopkins30-Oct-07 10:44
Lee hopkins30-Oct-07 10:44 
QuestionHow about Rowspan &gt; 2 ? Pin
CHAKCHAK17-May-05 21:24
CHAKCHAK17-May-05 21:24 
AnswerRe: How about Rowspan &gt; 2 ? Pin
mike_0122-May-05 23:07
mike_0122-May-05 23:07 
GeneralRe: How about Rowspan &gt; 2 ? Pin
gdfgdfg25-Jul-05 18:00
gdfgdfg25-Jul-05 18:00 
GeneralSimplification Pin
hastlaug21-Apr-05 13:36
hastlaug21-Apr-05 13:36 
GeneralThere is a bug when grid has invisible columns Pin
dududu3-Apr-05 21:02
dududu3-Apr-05 21:02 
GeneralRe: There is a bug when grid has invisible columns Pin
navinkaus11-Jan-06 21:56
navinkaus11-Jan-06 21:56 
GeneralItemDataBound is giving casting exception Pin
Anonymous17-Mar-05 17:02
Anonymous17-Mar-05 17:02 
GeneralWhere Can I Find this code in VB.net Pin
Ricardo Martinez22-Jan-05 19:45
Ricardo Martinez22-Jan-05 19:45 
GeneralRe: Where Can I Find this code in VB.net Pin
Rodrigo Pires1-Feb-05 6:31
Rodrigo Pires1-Feb-05 6:31 
GeneralRe: Where Can I Find this code in VB.net Pin
Ghalib Hamidi1-Oct-06 17:40
Ghalib Hamidi1-Oct-06 17:40 
GeneralBig problem Pin
ddtudor2613-Jan-05 2:52
ddtudor2613-Jan-05 2:52 
GeneralRe: Big problem Pin
geyond27-Jan-05 20:01
geyond27-Jan-05 20:01 
GeneralRe: Big problem (Solution) Pin
Peter Schmid11-Feb-05 7:26
Peter Schmid11-Feb-05 7:26 
GeneralRe: Big problem (Solution) Pin
Tumurbaatar28-Nov-05 17:55
Tumurbaatar28-Nov-05 17:55 

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

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