GridView with merged cells

I need to display data in grid view with merged rows for some columns. Please help me to prepare a grid view in below defined format:
enter image description here

And the original data comes from database is in below format:
enter image description here

Please help me to find best way for doing this task dynamically and efficiently.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You will have to use RowSpan.

Refer following code for it:

protected void GridView1_DataBound1(object sender, EventArgs e)
  for (int rowIndex = GridView1.Rows.Count - 2;
                                     rowIndex >= 0; rowIndex--)
    GridViewRow gvRow = GridView1.Rows[rowIndex];
    GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
    for (int cellCount = 0; cellCount < gvRow.Cells.Count;
     if (gvRow.Cells[cellCount].Text ==
       if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
         gvRow.Cells[cellCount].RowSpan = 2;
        gvRow.Cells[cellCount].RowSpan =
            gvPreviousRow.Cells[cellCount].RowSpan + 1;
       gvPreviousRow.Cells[cellCount].Visible = false;


Pictorial Example As In Question:

Method 2

Simplest way to merge Row-cells of first column is as below. Please note that For Loop is always to be iterated in reverse.

protected void GridView1_DataBound(object sender, EventArgs e)
        int RowSpan = 2;
        for (int i = GridView1.Rows.Count-2; i >=0 ;i-- )
            GridViewRow currRow = GridView1.Rows[i];
            GridViewRow prevRow = GridView1.Rows[i+1];
            if (currRow.Cells[0].Text == prevRow.Cells[0].Text)
                currRow.Cells[0].RowSpan = RowSpan;
                prevRow.Cells[0].Visible = false;
                RowSpan += 1;
                RowSpan = 2;

If you want to merge row-cells of all columns similarly, you can use another “forloop” within outer forloop written above

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x