Click here to Skip to main content
15,881,600 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 270.8K   6.3K   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

 
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 
Something like this:

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 cellIndex = 0; cellIndex < row.Cells.Count; cellIndex++)
            {
                string text1 = row.Cells[cellIndex] is DataControlFieldCell && ((DataControlFieldCell)row.Cells[cellIndex]).ContainingField is HyperLinkField
                    ? ((HyperLink)(row.Cells[cellIndex].Controls[0])).Text
                    : row.Cells[cellIndex].Text;

                string text2 = previousRow.Cells[cellIndex] is DataControlFieldCell && ((DataControlFieldCell)previousRow.Cells[cellIndex]).ContainingField is HyperLinkField
                    ? ((HyperLink)(previousRow.Cells[cellIndex].Controls[0])).Text
                    : previousRow.Cells[cellIndex].Text;

                if (text1 == text2)
                {
                    row.Cells[cellIndex].RowSpan = previousRow.Cells[cellIndex].RowSpan < 2 ? 2 : previousRow.Cells[cellIndex].RowSpan + 1;
                    previousRow.Cells[cellIndex].Visible = false;
                }
            }
        }
    }
}

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.