Click here to Skip to main content
Licence CPOL
First Posted 20 Mar 2009
Views 58,479
Downloads 1,402
Bookmarked 78 times

How to merge cells with equal values in a GridView

By | 20 Mar 2009 | Article
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)

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
GeneralMy vote of 5 Pinmembervshal5265:18 17 Mar '12  
QuestionGood One PinmemberDorababu74323:25 15 Jan '12  
AnswerRe: Good One PinmemberMykola Tarasyuk23:52 15 Jan '12  
GeneralRe: Good One PinmemberDorababu7430:03 16 Jan '12  
GeneralRe: Good One PinmemberMykola Tarasyuk3:47 16 Jan '12  
GeneralRe: Good One PinmemberDorababu7433:49 16 Jan '12  
GeneralRe: Good One PinmemberMykola Tarasyuk4:32 16 Jan '12  
GeneralRe: Good One PinmemberDorababu7435:12 16 Jan '12  
GeneralMy vote of 5 PinmemberDorababu74322:51 15 Jan '12  
GeneralMy vote of 5 Pinmemberramakrishnankt23:22 21 Dec '11  
Generalhave 5 PinmemberPranay Rana21:41 16 Jan '11  
GeneralMy vote of 5 Pinmembervhkvemuri3:20 19 Nov '10  
Generalmerge cells in Windows Application Using WPF DataGrid. Pinmembertripathy.rajendra@gmail.com20:35 28 Sep '10  
GeneralRe: merge cells in Windows Application Using WPF DataGrid. PinmemberMykola Tarasyuk3:53 29 Sep '10  
QuestionAlternating Colors? Pinmembercas20005:04 19 May '09  
AnswerRe: Alternating Colors? PinmemberMykola Tarasyuk19:48 19 May '09  
Questionhow can this be done for vs2003 Pinmemberiluvtorrent21:01 15 Apr '09  
AnswerRe: how can this be done for vs2003 PinmemberMykola Tarasyuk8:20 16 Apr '09  
GeneralRe: how can this be done for vs2003 Pinmemberiluvtorrent22:06 16 Apr '09  
GeneralMy vote of 1 Pinmemberkeertiraj5553:30 9 Apr '09  
GeneralRe: My vote of 1 PinmemberMykola Tarasyuk6:04 9 Apr '09  
GeneralRe: My vote of 1 Pinmembergstolarov18:45 9 Apr '09  
GeneralRe: My vote of 1 PinmemberMykola Tarasyuk19:31 9 Apr '09  
GeneralRe: [SORRY] My vote of 1 Pinmemberkeertiraj5550:09 11 Apr '09  
GeneralRe: [SORRY] My vote of 1, now 4 from me Pinmemberkeertiraj5550:25 11 Apr '09  
I'm really sorry, i've made a big mistake. Rose | [Rose] 4 from me
 
gstolarov wrote:
I suspect he created more then 1 account - I saw other votes at the same time with the same syntax and grammatical errors from the accounts opened today. I also saw something similar happening before I wish there would be a way to block those guys and remove their voting - they just discourage people who make an effort to share their knowledge.By the way great article, elegant solutions - thumbs up and 5 from me!

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.120604.1 | Last Updated 20 Mar 2009
Article Copyright 2009 by Mykola Tarasyuk
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid