Click here to Skip to main content
15,920,503 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 273.2K   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

 
Questionregarding merging Pin
subhas.a17-Sep-12 21:27
subhas.a17-Sep-12 21:27 
AnswerRe: regarding merging Pin
royalrao30-Dec-12 17:51
royalrao30-Dec-12 17:51 
AnswerRe: regarding merging Pin
royalrao30-Dec-12 17:51
royalrao30-Dec-12 17:51 
GeneralMy vote of 5 Pin
vshal52617-Mar-12 5:18
vshal52617-Mar-12 5:18 
QuestionGood One Pin
demouser74315-Jan-12 23:25
demouser74315-Jan-12 23:25 
AnswerRe: Good One Pin
Mykola Tarasyuk15-Jan-12 23:52
Mykola Tarasyuk15-Jan-12 23:52 
GeneralRe: Good One Pin
demouser74316-Jan-12 0:03
demouser74316-Jan-12 0:03 
GeneralRe: Good One Pin
Mykola Tarasyuk16-Jan-12 3:47
Mykola Tarasyuk16-Jan-12 3:47 
>> to merge only first cell
What does it mean "to merge only first cell"? How can you merge single cell? Does it mean merging cells in the first column? Could you be more concrete?
GeneralRe: Good One Pin
demouser74316-Jan-12 3:49
demouser74316-Jan-12 3:49 
GeneralRe: Good One Pin
Mykola Tarasyuk16-Jan-12 4:32
Mykola Tarasyuk16-Jan-12 4:32 
GeneralRe: Good One Pin
demouser74316-Jan-12 5:12
demouser74316-Jan-12 5:12 
GeneralMy vote of 5 Pin
demouser74315-Jan-12 22:51
demouser74315-Jan-12 22:51 
GeneralMy vote of 5 Pin
ramakrishnankt21-Dec-11 23:22
ramakrishnankt21-Dec-11 23:22 
Generalhave 5 Pin
Pranay Rana16-Jan-11 21:41
professionalPranay Rana16-Jan-11 21:41 
GeneralMy vote of 5 Pin
vhkvemuri19-Nov-10 3:20
vhkvemuri19-Nov-10 3:20 
Generalmerge cells in Windows Application Using WPF DataGrid. Pin
tripathy.rajendra@gmail.com28-Sep-10 20:35
tripathy.rajendra@gmail.com28-Sep-10 20:35 
GeneralRe: merge cells in Windows Application Using WPF DataGrid. Pin
Mykola Tarasyuk29-Sep-10 3:53
Mykola Tarasyuk29-Sep-10 3:53 
QuestionAlternating Colors? Pin
cas200019-May-09 5:04
cas200019-May-09 5:04 
AnswerRe: Alternating Colors? Pin
Mykola Tarasyuk19-May-09 19:48
Mykola Tarasyuk19-May-09 19:48 
Questionhow can this be done for vs2003 Pin
iluvtorrent15-Apr-09 21:01
iluvtorrent15-Apr-09 21:01 
AnswerRe: how can this be done for vs2003 Pin
Mykola Tarasyuk16-Apr-09 8:20
Mykola Tarasyuk16-Apr-09 8:20 
GeneralRe: how can this be done for vs2003 Pin
iluvtorrent16-Apr-09 22:06
iluvtorrent16-Apr-09 22:06 
GeneralMy vote of 1 Pin
keertiraj5559-Apr-09 3:30
keertiraj5559-Apr-09 3:30 
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 

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.