Click here to Skip to main content
Click here to Skip to main content

Cell Merging of DataGrid in ASP.NET

By , 10 Oct 2007
 

Introduction

This article describes how to merge adjacent cells in a Datagrid control in ASP.NET, hence gain better look and clarity for a web page. There are several scenarios in web applications 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 will 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 figure below shows the DataGrid without cell merging. The DataGrid has 6 rows 8 columns. Here, the data on the Employee Id and Employee Name columns are repeating in two rows, i.e., these are common to adjacent rows, and the rest of the data in each row is unique.

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

Screenshot - Image_1.jpg

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

After merging the Employee Id and Employee name columns, the DataGrid looks like:

Screenshot - Image_3.jpg

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

Cell merge can be done during the ItemDataBound event of the DataGrid. The RowSpan property of Cell is used to merge cells together. Actually, this activity merges cells and displays the data on that, and pushes the existing data forward so that the data is moved to the next adjacent cell. Now the extra data needs to be removed from the DataGrid. The 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 sets the Rowspan property of the cells, i.e., Employee Id (e.Item.Cells[0]) and Employee name (e.Item.Cells[2]).

Conclusion

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

License

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

About the Author

Deepthi Viswanathan Nair
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalif we don't know the number of repeat previouslymemberhasan.rounak14 Sep '10 - 0:56 
In the above case we know how many time one data will repeat.....But if we don't know the number of repeat previously what will be the approach??
Questionanybody send the mail please.membersenthilvinayagan12 Oct '09 - 1:53 
if any body know, how to merge the row cell in wpf datagrid.if the table column values are same and the another doubt is, how to hide the retrived column in the wpf datagrid. anybody know please send mail me. :|
Generalvrrificationmemberpune30 Sep '08 - 2:35 
'Loads given Assembly
Dim objAssembly As Assembly = Assembly.LoadFrom(paramPathDLL)
 
'Gets Types from given Assembly
Dim arrType As Type() = objAssembly.GetTypes()
 
'Type Type1 = objAssembly.GetType("SPDetails.SPDetailsProviderNew", true, true);
 
'Declaration variable to to get methods information
Dim arrMethodInfo As MethodInfo() = Nothing
 
'ArrayList object set to null
'objArrayList = null;
 
'Check for each Types found in Assembly
For Each objType As Type In arrType
'Checks given class name exist
If objType.Name = paramClassName Then
'Gets all methods for given class
arrMethodInfo = objType.GetMethods(BindingFlags.[Public] Or BindingFlags.Instance Or BindingFlags.DeclaredOnly Or BindingFlags.NonPublic Or BindingFlags.[Static])
 
'Checks class having methods
If arrMethodInfo.Length <> 0 Then
'Set Boolean to false when methods not found in class
blnStatus = True
Exit For
End If
Exit For
End If
Next
 

If blnStatus Then
'Checks for each methods for parameters
For Each objMethodInfo As MethodInfo In arrMethodInfo
If Not objMethodInfo.ToString().Contains("get_") AndAlso Not objMethodInfo.ToString().Contains("set_") Then
'Splitting staring to get method without return values
'Dim strArr As String() = objMethodInfo.ToString().Split(New Char() {" "c})
 
'Replacing unwanted string by '' and adding required string to ArrayList
objArrayList.Add(objMethodInfo.Name)
End If
Next
 
End If
AnswerRe: merge for more than 2 rowsmemberDeepthi Viswanathan Nair29 Sep '08 - 6:55 
you can change the RowSpan property to the number of rows to be merged and make corresponding change to the grdEmployee_ItemDataBound method
 
Thx
Deepthi V

Questioncan this be done w/gridview too?memberEdelman16 Oct '07 - 2:21 
doesn't help a lot of people if this doesn't work with gridview as well. if it does tho, very neat trick Smile | :)
 

AnswerRe: can this be done w/gridview too?memberMike Flavin15 Nov '07 - 0:18 
I just tried this variation with the GridView and it worked.
 
if (e.Row.DataItemIndex % 2 == 0)
{
e.Row.Cells[0].RowSpan = 2;
e.Row.Cells[1].RowSpan = 2;
}
// Remove the extra cells created due to row span for odd rows
if (e.Row.DataItemIndex % 2 == 1)
{
e.Row.Cells.RemoveAt(0);
e.Row.Cells.RemoveAt(0);
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
}

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 10 Oct 2007
Article Copyright 2007 by Deepthi Viswanathan Nair
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid