Click here to Skip to main content
6,595,444 members and growing! (20,082 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General

Merging Datagrid Header Column

By Girish_Misra

This article shows how to merge datgrid header columns
Posted:7 Jan 2007
Views:8,799
Bookmarked:7 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 1.45 Rating: 1.86 out of 5
4 votes, 66.7%
1
1 vote, 16.7%
2
1 vote, 16.7%
3

4

5

Introduction

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  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 http://www.codeproject.com/script/Articles/list_articles.asp?userid=191443 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.

Modifications

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.

<pre>

for (int i = 0; i < ctl.Controls.Count; i++)

{

if (asd.Contains(i))

{

TableCell item = (TableCell)asd[i];

item.RenderControl(writer);

i += (item.ColumnSpan-1);

}

else

{

ctl.Controls[i].RenderControl(writer);

}

}

</pre>

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). 

<pre>

Hashtable asd = new Hashtable();

public void ColMerge(string name, int index,int span)

{

TableCell cell = new TableCell();

cell.Text = name;

cell.ColumnSpan = span;

cell.HorizontalAlign = HorizontalAlign.Center;

asd .Add(index,cell);

}

</pre>

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.

<pre>

private ASPNetDatagridDecorator obj = new ASPNetDatagridDecorator();

TableCell cell = null;

DataGrid1.DataSource = GetData();

DataGrid1.DataBind();

obj.DatagridToDecorate = Datagrid2;

obj.ColMerge("Name", 1, 3);

m_add.merge("School", 5, 3);

Datagrid2.DataSource = GetData();

Datagrid2.DataBind();

</pre> 

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

License

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

Girish_Misra


Member

Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralError Pinmembersatejprabhu23:26 25 Feb '07  
GeneralRe: Error PinmemberGirish_Misra4:53 26 Feb '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 7 Jan 2007
Editor:
Copyright 2007 by Girish_Misra
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project