Click here to Skip to main content
6,634,665 members and growing! (19,702 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Cell Merging of DataGrid in ASP.NET

By Deepthi Viswanathan Nair

This article depicts how to merge cells in a Datagrid control of ASP.NET, hence gains better look and clarity.
C# 1.0.NET 1.1, Win2K, WinXP, Win2003, ASP.NET, WebForms, IIS 5.1, IIS 6, VS.NET2003, IE 6.0, IE 5.5, IE 7, IIS 7, Architect, Dev, Design
Posted:10 Oct 2007
Views:21,715
Bookmarked:28 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: 2.90 Rating: 3.73 out of 5

1
1 vote, 16.7%
2
1 vote, 16.7%
3

4
4 votes, 66.7%
5

Introduction

This article describes how to merge adjacent cells in a Datagrid control of ASP.NET, hence gains better look and clarity of web page. There are several scenarios in web application which can have some common values repeating in multiple rows. Usually these common data display against a set of unique data in other columns and creates a feeling of repeating the data.

In this article I describe how to eliminate these redundant data in each row and merge the cells together and display the data on the cells.

Using the code

The below figure shows the DataGrid with out cell merging. The DataGrid has 6 rows 8 columns. Here the data on Employee Id and Employee Name columns are repeating in 2 rows, ie, these are common to the adjacent rows and the rest of the data in each row is unique.

The red circle on the figure shows 1001, Nikhil Vinod repeated two times. The rest of the data on these rows are unique. We can see that the same pattern repeates in the rest of the rows of the DataGrid.

Screenshot - Image_1.jpg

The cell merging of DataGrid can be applied in this scenario. We can merge Employee Id and Employee name cells of adjacent rows, and display the data on the merged cell of DataGrid.

After merging the employee Id and Employee name columns of the DataGrid look like as follows:

Screenshot - Image_3.jpg

Now data on Employee ID, Employee Name for each group has been merged and displayed. We got very good look of data on the DataGrid.

Cell merge can be done during the ItemDataBound event of DataGrid. RowSpan property of Cell used to merge the cells together. Actually this activity merges the cells and displays the data on that and pushes the existing data forward, so that the data moved to next adjacent cell. Now the extra data needs to be removed from the DataGrid. Remove or RemoveAt property of the Cell can be used to remove the data from the DataGrid.

protected void grdEmployee_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if (chkChangetoNormal.Checked)
       {
       // Apply Row span for all even rows

         if (e.Item.ItemIndex % 2 == 0 )
       {
        e.Item.Cells[0].RowSpan = 2;
        e.Item.Cells[1].RowSpan = 2;
       }
       // Remove the extra cells created due to row span for odd rows

       if (e.Item.ItemIndex % 2 == 1 )
       {
        e.Item.Cells.RemoveAt(0); 
        e.Item.Cells.RemoveAt(0); 
        e.Item.Cells[1].HorizontalAlign = HorizontalAlign.Center ;
       }
    }
}

e.Item.ItemIndex represents the row id of the DataGrid, The First code snippet set the Rowspan property of the cells, ie. Employee Id (e.Item.Cells[0]) and Employee name (e.Item.Cells[2]).

Conclusion

It's most simplest way of merging cells in a DataGrid control of ASP.NET.

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

Deepthi Viswanathan Nair


Member

Location: United States United States

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 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Questionanybody send the mail please. Pinmembersenthilvinayagan2:53 12 Oct '09  
Generalvrrification Pinmemberpune3:35 30 Sep '08  
Questionmerge for more than 2 rows Pinmembermarzieh minooyee21:24 21 Sep '08  
AnswerRe: merge for more than 2 rows PinmemberDeepthi Viswanathan Nair7:55 29 Sep '08  
Generalcan this be done w/gridview too? PinmemberEdelman3:21 16 Oct '07  
GeneralRe: can this be done w/gridview too? PinmemberMike Flavin1:18 15 Nov '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 10 Oct 2007
Editor:
Copyright 2007 by Deepthi Viswanathan Nair
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project