Click here to Skip to main content
15,890,512 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 271.4K   6.4K   98   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: 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 
I was able to get it to work by using the RowDataBound event. I added a hidden databound column (column 0).   I keyed off that colunn to determine when to merge my hyperlink field column (column 1).   This is kinda specfic to my needs, but you should be able to adapt it to more general use if you want.   I will give your new code a try when I have a chance. Either way your project was helpful to me.   Thanks - scott  

            GridView grid = (GridView)sender;
            if (e.Row.RowIndex > 1)
            {                 
                  int x = e.Row.RowIndex - 1;
                  while ((x > 0) && (grid.Rows[x].Cells[1].Visible == false))
                  {
                        x--;
                  }                 
                  if (e.Row.Cells[0].Text == grid.Rows[x].Cells[0].Text)
                  {
                        if (grid.Rows[x].Cells[1].RowSpan < 2) grid.Rows[x].Cells[1].RowSpan = 2;
                        else grid.Rows[x].Cells[1].RowSpan++;
                        e.Row.Cells[1].Visible = false;
                  }
            }
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.