Click here to Skip to main content
Licence 
First Posted 19 Nov 2004
Views 65,990
Bookmarked 26 times

Grouping Rows in a DataGrid

By | 26 Apr 2005 | Article
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:

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

About the Author

BriOnH

Web Developer

United States United States

Member

Web Programmer / Analyst for Integres.com, and part owner www.golfbagstore.com

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberHristo Bojilov4:53 27 Aug '09  
GeneralLast row in datagrid does not group [modified] PinsussJohn hazin4:58 10 Dec '07  
GeneralRe:Grouping Rows in a DataGrid PinmemberVolcano__6:46 12 May '07  
GeneralFixed and understandible Pinmembermgrisoli6:18 8 Jan '07  
GeneralLast Row Problem solved .. [modified] PinmemberVMSSanthosh19:39 20 Sep '06  
using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
namespace GMIProject.GMIBuisinessLayer
{
///
/// Summary description for DataGridGrouper.
///

public class DataGridGrouper
{
public DataGridGrouper()
{
//
// TODO: Add constructor logic here
//
}
 
public void GroupColumn(DataGrid dg, int ColumnIndex)
{
int ItemIndex = 0;
int Groupings = 0;

if(dg.Items.Count>1)
{
foreach (DataGridItem dgi in dg.Items)
{
if (dgi.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 (dgi.Cells[ColumnIndex].Text ==
dg.Items[dgi.ItemIndex-1].Cells[ColumnIndex].Text)
{
dgi.Cells[ColumnIndex].Visible = false;
dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan =
dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1;
Groupings++;
}
else if (dg.Items[dgi.ItemIndex-1].Cells[ColumnIndex].Visible)
{
ItemIndex = dgi.ItemIndex;
}
else
{
dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan =
dg.Items[ItemIndex].Cells[ColumnIndex].RowSpan + 1;
ItemIndex = dgi.ItemIndex;

}
}
}

dg.Items[dg.Items.Count-2].Cells[ColumnIndex].RowSpan =
dg.Items[dg.Items.Count-2].Cells[ColumnIndex].RowSpan + 1;
//dg.Items[dg.Items.Count-1].Visible = false;

//return Groupings;
}
}
}
}
 

 
VMSSanthosh M.sc.,MCP.,CGT.,
WebDeveloper
Hysis Technologies Pvt Ltd
Bangalore.

GeneralThank you! PinmemberShivam Ray13:42 16 Aug '06  
GeneralMy implementation with stylesheet support Pinmemberkoedoot0:04 15 Mar '06  
QuestionWhat´s going on with the las row? PinmemberFrancisko9:24 20 Jul '05  
AnswerRe: What&#180;s going on with the las row? PinmemberSpithas22:39 2 Nov '05  
AnswerRe: What's going on with the last row? PinmemberRoyPardee11:22 5 Jul '06  
GeneralRe: What's going on with the last row? Pinmembermgrisoli6:16 8 Jan '07  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 26 Apr 2005
Article Copyright 2004 by BriOnH
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid