Click here to Skip to main content
6,629,885 members and growing! (22,125 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Grouping Rows in a DataGrid

By BriOnH

Grouping Rows in a DataGrid using C# and ASP.NET.
C#, .NET, Win2003, ASP.NET, VS.NET2003, Dev
Posted:19 Nov 2004
Updated:26 Apr 2005
Views:45,425
Bookmarked:23 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 2.13 Rating: 1.64 out of 5
14 votes, 70.0%
1

2
1 vote, 5.0%
3
1 vote, 5.0%
4
4 votes, 20.0%
5

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


Member
Web Programmer / Analyst for Integres.com, and part owner www.golfbagstore.com
Occupation: Web Developer
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 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberHristo Bojilov5:53 27 Aug '09  
GeneralLast row in datagrid does not group [modified] PinsussJohn hazin5:58 10 Dec '07  
GeneralRe:Grouping Rows in a DataGrid PinmemberVolcano__7:46 12 May '07  
GeneralFixed and understandible Pinmembermgrisoli7:18 8 Jan '07  
GeneralLast Row Problem solved .. [modified] PinmemberVMSSanthosh20:39 20 Sep '06  
GeneralThank you! PinmemberShivam Ray14:42 16 Aug '06  
GeneralMy implementation with stylesheet support Pinmemberkoedoot1:04 15 Mar '06  
GeneralWhat´s going on with the las row? PinmemberFrancisko10:24 20 Jul '05  
GeneralRe: What&#180;s going on with the las row? PinmemberSpithas23:39 2 Nov '05  
GeneralRe: What's going on with the last row? PinmemberRoyPardee12:22 5 Jul '06  
GeneralRe: What's going on with the last row? Pinmembermgrisoli7:16 8 Jan '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 26 Apr 2005
Editor:
Copyright 2004 by BriOnH
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project