Click here to Skip to main content
12,689,801 members (25,519 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked

Merging Datagrid Header Column

, 7 Jan 2007
Rate this:
Please Sign up or sign in to vote.
This article shows how to merge datgrid header columns


Hi all,

     This is my first article in Codeproject. Fortunately I dont have to code the whole process, much of the work has already been done <A name=> by irwansyah.

All the credits goes to him, he has done a wonderfull job of going through the pain of how the datgrid rendering works. I just studied his code and modified only a few parts so that it can be programmer friendly . So go to the page and first of all study the whole code and download the files. I will be using those files only.

This refers only to the second datagrid.What I will do is instead of specyfying each cell manually in the page_load method in the WebForm1.aspx.cs, I will use a function ColMerge() to specify only the columns that are needed to merge.I will use only the ASPNetDatagridDecorator.cs file.


First of all I will be modifying the ASPNetDatagridDecorator.cs file. Go to the NewRenderMethod() and delete everything inside it and add the following code.


<FONT color=#0000ff size=2>

for (<FONT color=#0000ff size=2>int i = 0; i < ctl.Controls.Count; i++)


<FONT color=#0000ff size=2>if (asd.Contains(i))


<FONT color=#008080 size=2>TableCell item = (<FONT color=#008080 size=2>TableCell)asd[i];


i += (item.ColumnSpan-1);


<FONT color=#0000ff size=2>else






The above code loops through all the default datagrid tablecell controls represented by ctl and renders them. The condition is used to check if a custom table cell has been used or not for having merged datagrid headers.

Then add the following code in an independent part of the file(in the class). 


<FONT color=#008080 size=2>

Hashtable asd = <FONT color=#0000ff size=2>new <FONT color=#008080 size=2>Hashtable();

<FONT color=#0000ff size=2>public <FONT color=#0000ff size=2>void ColMerge(<FONT color=#0000ff size=2>string name, <FONT color=#0000ff size=2>int index,<FONT color=#0000ff size=2>int span)


<FONT color=#008080 size=2>TableCell cell = <FONT color=#0000ff size=2>new <FONT color=#008080 size=2>TableCell();

cell.Text = name;

cell.ColumnSpan = span;

cell.HorizontalAlign = <FONT color=#008080 size=2>HorizontalAlign.Center;

asd .Add(index,cell);



This code had a method which takes the common name of the merged grid header and stores it in the asd hastable to be used in the NewRenderMethod.

No need to modify any thing else in this file.

Now move on to the Webform1.aspx.cs file. In this file under the page-load method write the following code.


<FONT color=#0000ff size=2>

private <FONT color=#008080 size=2>ASPNetDatagridDecorator obj = <FONT color=#0000ff size=2>new <FONT color=#008080 size=2>ASPNetDatagridDecorator();

<FONT color=#008080 size=2>

TableCell cell = <FONT color=#0000ff size=2>null;

DataGrid1.DataSource = GetData();


obj.DatagridToDecorate = Datagrid2;

obj.ColMerge(<FONT color=#800000 size=2>"Name", 1, 3);

m_add.merge(<FONT color=#800000 size=2>"School", 5, 3);

Datagrid2.DataSource = GetData();



You can add as many ColMerge methods you like in a single datagrid.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralError Pin
satejprabhu25-Feb-07 23:26
membersatejprabhu25-Feb-07 23:26 
GeneralRe: Error Pin
Girish_Misra26-Feb-07 4:53
memberGirish_Misra26-Feb-07 4:53 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 7 Jan 2007
Article Copyright 2007 by Girish_Misra
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid