Click here to Skip to main content
16,012,082 members
Articles / Programming Languages / C#

How to Merge Cells with Equal Values in a GridView

Rate me:
Please Sign up or sign in to vote.
4.87/5 (48 votes)
20 Mar 2009CPOL 277.7K   6.4K   99   63
My solution is not the first; however, I think, it is rather universal and very short - less than 20 lines of the code...
In this tip, you will learn a universal and short method to merge GridView rows if neighboring cells show equal values.

Introduction

There are a lot of methods on the Internet solving the problem of how to merge GridView rows if neighboring cells show equal values. My approach is not the first; however, I think, it is rather universal and very short - less than 20 lines of code.

Image 1

The algorithm is simple: to bypass all the rows, starting from the second at the bottom, to the top. If a cell value is the same as a value in the previous (lower) row, then increase RowSpan and make the lower cell invisible, and so forth.

The code that merges the cells is very short:

C#
public class GridDecorator
{
    public static void MergeRows(GridView gridView)
    {
        for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow row = gridView.Rows[rowIndex];
            GridViewRow previousRow = gridView.Rows[rowIndex + 1];

            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (row.Cells[i].Text == previousRow.Cells[i].Text)
                {
                    row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : 
                                           previousRow.Cells[i].RowSpan + 1;
                    previousRow.Cells[i].Visible = false;
                }
            }
        }
    }
}

The last action is to add an OnPreRender event handler for the GridView:

C#
protected void gridView_PreRender(object sender, EventArgs e)
{
    GridDecorator.MergeRows(gridView);
}

History

  • 20th March, 2009: Initial version

License

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


Written By
Software Developer (Senior)
Ukraine Ukraine
I'm a software developer from Ukraine. I write about ASP.NET and other .NET related topics over at my blog

Comments and Discussions

 
GeneralRe: My vote of 1 Pin
Mykola Tarasyuk9-Apr-09 6:04
Mykola Tarasyuk9-Apr-09 6:04 
GeneralRe: My vote of 1 Pin
gstolarov9-Apr-09 18:45
gstolarov9-Apr-09 18:45 
GeneralRe: My vote of 1 Pin
Mykola Tarasyuk9-Apr-09 19:31
Mykola Tarasyuk9-Apr-09 19:31 
GeneralRe: [SORRY] My vote of 1 Pin
keertiraj55511-Apr-09 0:09
keertiraj55511-Apr-09 0:09 
GeneralRe: [SORRY] My vote of 1, now 4 from me Pin
keertiraj55511-Apr-09 0:25
keertiraj55511-Apr-09 0:25 
GeneralRe: My vote of 1 Pin
Shameel5-Jul-11 8:31
professionalShameel5-Jul-11 8:31 
GeneralGridview Grouping Pin
danishjee30-Mar-09 21:54
danishjee30-Mar-09 21:54 
GeneralRe: Gridview Grouping [modified] Pin
Mykola Tarasyuk31-Mar-09 0:38
Mykola Tarasyuk31-Mar-09 0:38 
Actually the above code does not group data, it only merges cells if they contain equal values. So if you manage to get data in this form
Sr#.   Item   Description      Weight   Color     Shape  Qty   Price
1      10002     ABC             2.0     Red       RJ     1     100
1      10002     ABC           21.02     Gray      KJ     1     100
2      10254     DEF             2.0     Green     A-J    2    2200
2      10254     DEF             2.9     White     L.S.   2    2200
2      10254     DEF            2.01     Melt      LO     2    2200

and execute GridDecorator.MergeRows(gridView) you will get something similar to the example you posted.

modified on Tuesday, March 31, 2009 7:01 AM

Generalproblem in the code Pin
AboElmnzer26-Mar-09 3:16
AboElmnzer26-Mar-09 3:16 
GeneralRe: problem in the code Pin
Mykola Tarasyuk26-Mar-09 9:28
Mykola Tarasyuk26-Mar-09 9:28 
QuestionRe: problem in the code Pin
akdn200929-Mar-09 19:03
akdn200929-Mar-09 19:03 
AnswerRe: problem in the code Pin
Mykola Tarasyuk30-Mar-09 3:30
Mykola Tarasyuk30-Mar-09 3:30 
GeneralRe: problem in the code [modified] Pin
Scott Way20-Aug-09 7:36
Scott Way20-Aug-09 7:36 
GeneralRe: problem in the code Pin
Scott Way20-Aug-09 9:19
Scott Way20-Aug-09 9:19 
GeneralRe: problem in the code Pin
Mykola Tarasyuk20-Aug-09 9:59
Mykola Tarasyuk20-Aug-09 9:59 
GeneralRe: problem in the code Pin
Mykola Tarasyuk20-Aug-09 10:31
Mykola Tarasyuk20-Aug-09 10:31 
GeneralRe: problem in the code Pin
Scott Way21-Aug-09 6:05
Scott Way21-Aug-09 6:05 
GeneralRe: problem in the code Pin
Scott Way21-Aug-09 6:17
Scott Way21-Aug-09 6:17 
GeneralIts Really Great!! Pin
Anurag Gandhi24-Mar-09 3:06
professionalAnurag Gandhi24-Mar-09 3:06 
GeneralJust what I was looking for ... Pin
IsmailLunat21-Mar-09 2:46
IsmailLunat21-Mar-09 2:46 

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.