Click here to Skip to main content
15,890,390 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

 
QuestionHiding only specific rows ? Pin
usernametakenalready9-Sep-13 22:47
usernametakenalready9-Sep-13 22:47 
AnswerRe: Hiding only specific rows ? Pin
Mykola Tarasyuk10-Sep-13 20:18
Mykola Tarasyuk10-Sep-13 20:18 
GeneralRe: Hiding only specific rows ? Pin
usernametakenalready11-Sep-13 1:05
usernametakenalready11-Sep-13 1:05 
QuestionBrilliant Pin
andy199213-Jul-13 21:37
andy199213-Jul-13 21:37 
QuestionBrilliant Pin
gyaan864-Apr-13 21:27
gyaan864-Apr-13 21:27 
AnswerRe: Brilliant Pin
Mykola Tarasyuk4-Apr-13 21:52
Mykola Tarasyuk4-Apr-13 21:52 
Questionsgrdjdsflk;gm Pin
Mukesh.Ce6215-Feb-13 23:48
Mukesh.Ce6215-Feb-13 23:48 
GeneralHow to add delete button to merge rows Pin
Santhosh239-Jan-13 3:13
Santhosh239-Jan-13 3:13 
GeneralMy vote of 5 Pin
Global Analyser5-Jan-13 16:51
Global Analyser5-Jan-13 16:51 
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 
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 

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.