Click here to Skip to main content
12,355,050 members (65,152 online)
Click here to Skip to main content
Add your own
alternative version

Stats

158.9K views
4.7K downloads
97 bookmarked
Posted

How to merge cells with equal values in a GridView

, 20 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
My solution is not the first; however, I think, it is rather universal and very short - less than 20 lines of the code...

Introduction

There are a lot of methods in 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.

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:

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:

protected void gridView_PreRender(object sender, EventArgs e)
{
    GridDecorator.MergeRows(gridView);
}

This is a cross-post from my personal blog...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mykola Tarasyuk
Software Developer (Senior)
Ukraine Ukraine
I am a software developer from Ukraine. In my spare time (this means "very rarely last years"), I am blogging about ASP.Net and web development.


You may also be interested in...

Comments and Discussions

 
QuestionCan't we use the same logic for Data Grid ? Pin
Member 115775256-Apr-15 20:52
memberMember 115775256-Apr-15 20:52 
AnswerRe: Can't we use the same logic for Data Grid ? Pin
Mykola Tarasyuk6-Apr-15 22:06
memberMykola Tarasyuk6-Apr-15 22:06 
QuestionProblem in merging cells with equal values in a GridView Pin
rahulDer16-Jul-14 21:48
memberrahulDer16-Jul-14 21:48 
AnswerRe: Problem in merging cells with equal values in a GridView Pin
Mykola Tarasyuk17-Jul-14 2:05
memberMykola Tarasyuk17-Jul-14 2:05 
QuestionHoe to do the same if we use MVVM pattern in our application. Pin
spins39-Apr-14 22:52
memberspins39-Apr-14 22:52 
GeneralThank You Pin
pirateworks4-Dec-13 2:30
memberpirateworks4-Dec-13 2:30 
QuestionHiding only specific rows ? Pin
oms0249-Sep-13 22:47
memberoms0249-Sep-13 22:47 
AnswerRe: Hiding only specific rows ? Pin
Mykola Tarasyuk10-Sep-13 20:18
memberMykola Tarasyuk10-Sep-13 20:18 
GeneralRe: Hiding only specific rows ? Pin
oms02411-Sep-13 1:05
memberoms02411-Sep-13 1:05 
QuestionBrilliant Pin
andy199213-Jul-13 21:37
memberandy199213-Jul-13 21:37 
QuestionBrilliant Pin
gyaan864-Apr-13 21:27
membergyaan864-Apr-13 21:27 
AnswerRe: Brilliant Pin
Mykola Tarasyuk4-Apr-13 21:52
memberMykola Tarasyuk4-Apr-13 21:52 
Questionsgrdjdsflk;gm Pin
MP Prajapati15-Feb-13 23:48
memberMP Prajapati15-Feb-13 23:48 
GeneralHow to add delete button to merge rows Pin
Santhosh239-Jan-13 3:13
memberSanthosh239-Jan-13 3:13 
GeneralMy vote of 5 Pin
Heaven20205-Jan-13 16:51
memberHeaven20205-Jan-13 16:51 
Questionregarding merging Pin
subhas.a17-Sep-12 21:27
membersubhas.a17-Sep-12 21:27 
AnswerRe: regarding merging Pin
raojack30-Dec-12 17:51
memberraojack30-Dec-12 17:51 
AnswerRe: regarding merging Pin
raojack30-Dec-12 17:51
memberraojack30-Dec-12 17:51 
GeneralMy vote of 5 Pin
vshal52617-Mar-12 5:18
membervshal52617-Mar-12 5:18 
QuestionGood One Pin
Dorababu74315-Jan-12 23:25
memberDorababu74315-Jan-12 23:25 
AnswerRe: Good One Pin
Mykola Tarasyuk15-Jan-12 23:52
memberMykola Tarasyuk15-Jan-12 23:52 
GeneralRe: Good One Pin
Dorababu74316-Jan-12 0:03
memberDorababu74316-Jan-12 0:03 
GeneralRe: Good One Pin
Mykola Tarasyuk16-Jan-12 3:47
memberMykola Tarasyuk16-Jan-12 3:47 
GeneralRe: Good One Pin
Dorababu74316-Jan-12 3:49
memberDorababu74316-Jan-12 3:49 
GeneralRe: Good One Pin
Mykola Tarasyuk16-Jan-12 4:32
memberMykola Tarasyuk16-Jan-12 4:32 
GeneralRe: Good One Pin
Dorababu74316-Jan-12 5:12
memberDorababu74316-Jan-12 5:12 
GeneralMy vote of 5 Pin
Dorababu74315-Jan-12 22:51
memberDorababu74315-Jan-12 22:51 
GeneralMy vote of 5 Pin
ramakrishnankt21-Dec-11 23:22
memberramakrishnankt21-Dec-11 23:22 
Generalhave 5 Pin
Pranay Rana16-Jan-11 21:41
memberPranay Rana16-Jan-11 21:41 
GeneralMy vote of 5 Pin
vhkvemuri19-Nov-10 3:20
membervhkvemuri19-Nov-10 3:20 
Generalmerge cells in Windows Application Using WPF DataGrid. Pin
tripathy.rajendra@gmail.com28-Sep-10 20:35
membertripathy.rajendra@gmail.com28-Sep-10 20:35 
GeneralRe: merge cells in Windows Application Using WPF DataGrid. Pin
Mykola Tarasyuk29-Sep-10 3:53
memberMykola Tarasyuk29-Sep-10 3:53 
QuestionAlternating Colors? Pin
cas200019-May-09 5:04
membercas200019-May-09 5:04 
AnswerRe: Alternating Colors? Pin
Mykola Tarasyuk19-May-09 19:48
memberMykola Tarasyuk19-May-09 19:48 
Questionhow can this be done for vs2003 Pin
iluvtorrent15-Apr-09 21:01
memberiluvtorrent15-Apr-09 21:01 
AnswerRe: how can this be done for vs2003 Pin
Mykola Tarasyuk16-Apr-09 8:20
memberMykola Tarasyuk16-Apr-09 8:20 
GeneralRe: how can this be done for vs2003 Pin
iluvtorrent16-Apr-09 22:06
memberiluvtorrent16-Apr-09 22:06 
GeneralMy vote of 1 Pin
keertiraj5559-Apr-09 3:30
memberkeertiraj5559-Apr-09 3:30 
GeneralRe: My vote of 1 Pin
Mykola Tarasyuk9-Apr-09 6:04
memberMykola Tarasyuk9-Apr-09 6:04 
GeneralRe: My vote of 1 Pin
gstolarov9-Apr-09 18:45
membergstolarov9-Apr-09 18:45 
GeneralRe: My vote of 1 Pin
Mykola Tarasyuk9-Apr-09 19:31
memberMykola Tarasyuk9-Apr-09 19:31 
GeneralRe: [SORRY] My vote of 1 Pin
keertiraj55511-Apr-09 0:09
memberkeertiraj55511-Apr-09 0:09 
GeneralRe: [SORRY] My vote of 1, now 4 from me Pin
keertiraj55511-Apr-09 0:25
memberkeertiraj55511-Apr-09 0:25 
GeneralRe: My vote of 1 Pin
Shameel5-Jul-11 8:31
memberShameel5-Jul-11 8:31 
GeneralGridview Grouping Pin
danishjee30-Mar-09 21:54
memberdanishjee30-Mar-09 21:54 
GeneralRe: Gridview Grouping [modified] Pin
Mykola Tarasyuk31-Mar-09 0:38
memberMykola Tarasyuk31-Mar-09 0:38 
Generalproblem in the code Pin
AboElmnzer26-Mar-09 3:16
memberAboElmnzer26-Mar-09 3:16 
GeneralRe: problem in the code Pin
Mykola Tarasyuk26-Mar-09 9:28
memberMykola Tarasyuk26-Mar-09 9:28 
QuestionRe: problem in the code Pin
akdn200929-Mar-09 19:03
memberakdn200929-Mar-09 19:03 
AnswerRe: problem in the code Pin
Mykola Tarasyuk30-Mar-09 3:30
memberMykola Tarasyuk30-Mar-09 3:30 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 20 Mar 2009
Article Copyright 2009 by Mykola Tarasyuk
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid