Click here to Skip to main content
Licence CPOL
First Posted 20 Mar 2009
Views 51,778
Downloads 990
Bookmarked 75 times

How to merge cells with equal values in a GridView

By Mykola Tarasyuk | 20 Mar 2009
My solution is not the first; however, I think, it is rather universal and very short - less than 20 lines of the code...

1

2
4 votes, 11.8%
3
6 votes, 17.6%
4
24 votes, 70.6%
5
4.83/5 - 34 votes
4 removed
μ 4.67, σa 1.23 [?]

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)

About the Author

Mykola Tarasyuk

Web Developer

Ukraine Ukraine

Member
I am a software developer from Ukraine. In my spare time, I am blogging about ASP.Net and web development.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGood One PinmemberDorababu7430:25 16 Jan '12  
AnswerRe: Good One PinmemberMykola Tarasyuk0:52 16 Jan '12  
GeneralRe: Good One PinmemberDorababu7431:03 16 Jan '12  
GeneralRe: Good One PinmemberMykola Tarasyuk4:47 16 Jan '12  
GeneralRe: Good One PinmemberDorababu7434:49 16 Jan '12  
GeneralRe: Good One PinmemberMykola Tarasyuk5:32 16 Jan '12  
GeneralRe: Good One PinmemberDorababu7436:12 16 Jan '12  
GeneralMy vote of 5 PinmemberDorababu74323:51 15 Jan '12  
GeneralMy vote of 5 Pinmemberramakrishnankt0:22 22 Dec '11  
Generalhave 5 PinmemberPranay Rana22:41 16 Jan '11  
GeneralMy vote of 5 Pinmembervhkvemuri4:20 19 Nov '10  
Generalmerge cells in Windows Application Using WPF DataGrid. Pinmembertripathy.rajendra@gmail.com21:35 28 Sep '10  
GeneralRe: merge cells in Windows Application Using WPF DataGrid. PinmemberMykola Tarasyuk4:53 29 Sep '10  
QuestionAlternating Colors? Pinmembercas20006:04 19 May '09  
AnswerRe: Alternating Colors? PinmemberMykola Tarasyuk20:48 19 May '09  
Questionhow can this be done for vs2003 Pinmemberiluvtorrent22:01 15 Apr '09  
AnswerRe: how can this be done for vs2003 PinmemberMykola Tarasyuk9:20 16 Apr '09  
GeneralRe: how can this be done for vs2003 Pinmemberiluvtorrent23:06 16 Apr '09  
GeneralMy vote of 1 Pinmemberkeertiraj5554:30 9 Apr '09  
GeneralRe: My vote of 1 PinmemberMykola Tarasyuk7:04 9 Apr '09  
GeneralRe: My vote of 1 Pinmembergstolarov19:45 9 Apr '09  
GeneralRe: My vote of 1 PinmemberMykola Tarasyuk20:31 9 Apr '09  
GeneralRe: [SORRY] My vote of 1 Pinmemberkeertiraj5551:09 11 Apr '09  
GeneralRe: [SORRY] My vote of 1, now 4 from me Pinmemberkeertiraj5551:25 11 Apr '09  
GeneralRe: My vote of 1 PinmemberShameel9:31 5 Jul '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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