Skip to main content

How to merge cells with equal values in the GridView

My solution is not the first; however, I think, it is rather universal and very short - less than 20 lines of the 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)

Download example

Download example for template field


  1. Hello.
    I have one question. What do " < 2 ? 2 : " it mean?
    Thank you.

    1. It's an old question but in case of someone go here....

      It's a ternary condition... {condtion} ? {Result if true}:{Result if false};
      For example here : "previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1"
      means :
      if(previousRow.Cells[i].RowSpan < 2 )
      row.Cells[i].RowSpan = 2;
      row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan + 1;

  2. This comment has been removed by a blog administrator.

  3. can you please make Paging for this gridview. and merged column records should not split into next page

  4. Hi MARSS. I need to merge cells that contain the same data horizontally on a gridview using asp .net. Can you help me? I trying using columnspan property but i have some issues.

  5. i have used templatefield in my gridview . It is showing me only first row. could you help me?

    1. In case of templatefield row.Cells[i].Text does not work. You should use something like this:

      I attached an example to the article.

  6. HI
    I have developed merge cells in repeater.. so can you help me please?

  7. how can i do it in windows form for

  8. Can u please help me in applying pagination and sorting method for this merged gridview

  9. Solution Completes my 95% of the requirement.

    I have some blank cells in my third Column;
    According To this Logic Those Empty Cells Shrink into Small cells(height).
    I Dont Want Them to Shring What shouid i Do?


Post a Comment

Popular posts from this blog

Merging columns in GridView/DataGrid header

As necessity to show header columns in a few rows occurs fairly often it would be good to have such functionality in the GridView/DataGrid control as an in-built feature. But meanwhile everyone solves this problem in his own way.

The described below variant of the merging implementation is based on irwansyah's idea to use the SetRenderMethodDelegate method for custom rendering of grid columns header. I guess this approach can be simplified in order to get more compact and handy code for reuse.
The code overview
As it may be required to merge a few groups of columns - for example, 1,2 and 4,5,6 - we need a class to store common information about all united columns.
private class MergedColumnsInfo
// indexes of merged columns
public List<int> MergedColumns = new List<int>();
// key-value pairs: key = the first column index, value = number of the merged columns
public Hashtable StartColumns = new Hashtable();
// key-value pairs: key = the first column in…

JIRA REST API: Cookie-based Authentication

Three authentication methods are proposed by the JIRA REST API documentation: Basic Authentication is a simple but not very safe approach. Credentials are sent in the header on every request and encoding to Base64 is not a proper protection in this case; HTTPS connection is required. OAuth authentication - looks a bit complex and requires additional configuration at the JIRA server that is not always possible. Cookie-based Authentication - this approach seems to be the most convinient one: credentials are checked once, then the authentication cookie only is sent on subsequent requests. However, trying to use the cookie-based authentication I encountered an issue. The approach described in the documentation worked partially: I was able to create a new session and get the response containing the session cookie but all subsequent requests using this session cookie were rejected as unauthorized. Spending some time investigating this I found the cause of the issue: JSESSIONID is not th…