Click here to Skip to main content
13,089,246 members (65,430 online)
Click here to Skip to main content
Add your own
alternative version

Stats

82.8K views
1.6K downloads
26 bookmarked
Posted 19 Nov 2004

Grouping Rows in a DataGrid

, 26 Apr 2005
Rate this:
Please Sign up or sign in to vote.
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
{
/// <span class="code-SummaryComment"><SUMMARY>
</span>/// Summary description for DataGridGrouper.
/// <span class="code-SummaryComment"></SUMMARY>
</span>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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Hristo Bojilov27-Aug-09 4:53
memberHristo Bojilov27-Aug-09 4:53 
GeneralLast row in datagrid does not group [modified] Pin
John hazin10-Dec-07 4:58
sussJohn hazin10-Dec-07 4:58 
GeneralRe:Grouping Rows in a DataGrid Pin
Volcano__12-May-07 6:46
memberVolcano__12-May-07 6:46 
GeneralFixed and understandible Pin
mgrisoli8-Jan-07 6:18
membermgrisoli8-Jan-07 6:18 
GeneralLast Row Problem solved .. [modified] Pin
VMSSanthosh20-Sep-06 19:39
memberVMSSanthosh20-Sep-06 19:39 
GeneralThank you! Pin
Shivam Ray16-Aug-06 13:42
memberShivam Ray16-Aug-06 13:42 
GeneralMy implementation with stylesheet support Pin
koedoot15-Mar-06 0:04
memberkoedoot15-Mar-06 0:04 
QuestionWhat&#180;s going on with the las row? Pin
Francisko20-Jul-05 9:24
memberFrancisko20-Jul-05 9:24 
AnswerRe: What&amp;#180;s going on with the las row? Pin
Spithas2-Nov-05 22:39
memberSpithas2-Nov-05 22:39 
AnswerRe: What's going on with the last row? Pin
RoyPardee5-Jul-06 11:22
memberRoyPardee5-Jul-06 11:22 
GeneralRe: What's going on with the last row? Pin
mgrisoli8-Jan-07 6:16
membermgrisoli8-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.

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