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

 
QuestionWhen i am using Template fields its not working. Pin
Member 1441730624-May-21 0:51
Member 1441730624-May-21 0:51 
QuestionChange font color Pin
Ciupaz12-Jun-20 19:11
Ciupaz12-Jun-20 19:11 
BugPlease change the code sample Pin
sadiqabbas23-May-19 22:43
professionalsadiqabbas23-May-19 22:43 
GeneralMy vote of 5 Pin
Karthik_Mahalingam12-Oct-17 19:52
professionalKarthik_Mahalingam12-Oct-17 19:52 
QuestionHow to export to excel the above same gridview data exactly Pin
sreeenivasa reddy12-Oct-17 19:10
sreeenivasa reddy12-Oct-17 19:10 
QuestionCan't we use the same logic for Data Grid ? Pin
Member 115775256-Apr-15 20:52
Member 115775256-Apr-15 20:52 
AnswerRe: Can't we use the same logic for Data Grid ? Pin
Mykola Tarasyuk6-Apr-15 22:06
Mykola Tarasyuk6-Apr-15 22:06 
QuestionProblem in merging cells with equal values in a GridView Pin
rahulDer16-Jul-14 21:48
rahulDer16-Jul-14 21:48 
AnswerRe: Problem in merging cells with equal values in a GridView Pin
Mykola Tarasyuk17-Jul-14 2:05
Mykola Tarasyuk17-Jul-14 2:05 
As a fast solution you can try this:

if (((Label)(row.Cells[cellIndex].Controls[1])).Text == ((Label)(previousRow.Cells[cellIndex].Controls[1])).Text)


Controls[1] should return Label from ItemTemplate. The more robust solution is either to use FindControl method to get the labels or implement an own method looking for controls of type Label within the item templates.
QuestionHoe to do the same if we use MVVM pattern in our application. Pin
spins39-Apr-14 22:52
spins39-Apr-14 22:52 
GeneralThank You Pin
pirateworks4-Dec-13 2:30
pirateworks4-Dec-13 2:30 
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 

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.