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

Grouping Rows in a DataGrid

Rate me:
Please Sign up or sign in to vote.
1.17/5 (20 votes)
26 Apr 2005 94K   1.7K   26   11
Grouping Rows in a DataGrid using C# and ASP.NET.

Introduction

This is built off the same idea that Serge Lobko-Lobanovsky had in his article on here at http://www.codeproject.com/aspnet/GridGroupFormat.asp.

had trouble with the code he had posted and had to get it done for a project. Just call the method DataGridGroupBy(DataGrid dgYouWantGrouped, int ColumnToGroup) of the DataGridGroupByRows class in your WebForm and it will group all rows you want grouped together.

Here is the code for a class i wrote in the sample project attached:

C#
using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace localhost
{
/// <SUMMARY>
/// Summary description for DataGridGrouper.
/// </SUMMARY>
public class DataGridGrouper
{
    public DataGridGrouper()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void GroupColumn(DataGrid dgMonitor, int ColumnIndex)
    {
        int ItemIndex= 0;

        foreach(DataGridItem dgItem in dgMonitor.Items)
        {
            if(dgItem.ItemIndex > 0)
            { //if current cells text is the same as the cell above it
                //make it invisible and increase the row span by 1 of the 
                //last visible cell in that column.
                if(dgItem.Cells[ColumnIndex].Text == 
                  dgMonitor.Items[dgItem.ItemIndex-1].Cells[ColumnIndex].Text)
                {
                    dgItem.Cells[ColumnIndex].Visible = false;
                    dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan = 
                    dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1;

                }
                else if(dgMonitor.Items[
                    dgItem.ItemIndex-1].Cells[ColumnIndex].Visible == true)
                {

                    ItemIndex = dgItem.ItemIndex;
                }
                else
                {
                    dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan = 
                     dgMonitor.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1;
                    ItemIndex = dgItem.ItemIndex;
                }
            }
        }
        //remove the extra row - might be good to know why i have to add an 
        //extra row.
        dgMonitor.Items[dgMonitor.Items.Count-1].Visible = false;
    }
}
}

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


Written By
Web Developer
United States United States
Web Programmer / Analyst for Integres.com, and part owner www.golfbagstore.com

Comments and Discussions

 
GeneralMy vote of 1 Pin
Hristo-Bojilov27-Aug-09 4:53
Hristo-Bojilov27-Aug-09 4:53 
GeneralLast row in datagrid does not group [modified] Pin
Strychtur10-Dec-07 4:58
Strychtur10-Dec-07 4:58 
GeneralRe:Grouping Rows in a DataGrid Pin
Volcano__12-May-07 6:46
Volcano__12-May-07 6:46 
GeneralFixed and understandible Pin
mgrisoli8-Jan-07 6:18
mgrisoli8-Jan-07 6:18 
GeneralLast Row Problem solved .. [modified] Pin
VMSSanthosh20-Sep-06 19:39
VMSSanthosh20-Sep-06 19:39 
GeneralThank you! Pin
Shivam Ray16-Aug-06 13:42
Shivam Ray16-Aug-06 13:42 
GeneralMy implementation with stylesheet support Pin
koedoot15-Mar-06 0:04
koedoot15-Mar-06 0:04 
QuestionWhat&#180;s going on with the las row? Pin
Francisko20-Jul-05 9:24
Francisko20-Jul-05 9:24 
AnswerRe: What&amp;#180;s going on with the las row? Pin
Spithas2-Nov-05 22:39
Spithas2-Nov-05 22:39 
AnswerRe: What's going on with the last row? Pin
RoyPardee5-Jul-06 11:22
RoyPardee5-Jul-06 11:22 
Yeah, that last row thing is evil--it just swallows it up. Here's what I came up with, in case it's any use to anyone.

Public Sub GroupColumn(ByVal myDataGrid As DataGrid, ByVal ColumnIndex As Integer)
Dim CellPointer As Integer = 0
Dim ThisCell As TableCell
Dim PreviousCell As TableCell
Dim PointedCell As TableCell

For Each dgi As DataGridItem In myDataGrid.Items
If dgi.ItemIndex > 0 Then
ThisCell = dgi.Cells(ColumnIndex)
PreviousCell = myDataGrid.Items(dgi.ItemIndex - 1).Cells(ColumnIndex)
PointedCell = myDataGrid.Items(CellPointer).Cells(ColumnIndex)

If ThisCell.Text = PreviousCell.Text Then
ThisCell.Visible = False
PointedCell.RowSpan += 1
Else
If PreviousCell.Visible = False Then
PointedCell.RowSpan += 1
End If
CellPointer = dgi.ItemIndex
End If
End If
Next
' Why is this necessary?
If Not (ThisCell Is Nothing OrElse PreviousCell Is Nothing OrElse PointedCell Is Nothing) Then
If ThisCell.Text = PreviousCell.Text Then
PointedCell.RowSpan += 1
End If
End If
End Sub

I'm not actually sure why that last bit is necessary, but it seems to be...
GeneralRe: What's going on with the last row? Pin
mgrisoli8-Jan-07 6:16
mgrisoli8-Jan-07 6:16 

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.