17

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.

himanshu
  • 442
  • 2
  • 7
  • 17

2 Answers2

19

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;
                                                  cellCount++)
    {
     if (gvRow.Cells[cellCount].Text ==
                            gvPreviousRow.Cells[cellCount].Text)
     {
       if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
       {
         gvRow.Cells[cellCount].RowSpan = 2;
       }
       else
       {
        gvRow.Cells[cellCount].RowSpan =
            gvPreviousRow.Cells[cellCount].RowSpan + 1;
       }
       gvPreviousRow.Cells[cellCount].Visible = false;
    }
   }
}

Referance:

https://sites.google.com/site/learning6329/asp-net/gridview-merge-cells

Pictorial Example As In Question:

http://marss.co.ua/MergingCellsInGridView.aspx

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • Thanks for your help, this code works exactly as per my requirement. – himanshu Apr 23 '13 at 05:43
  • One more thing I need to ask, is there any way to limit this cell merging for fix columns not for all columns of grid view. For example as in my sample table I have provided in question, shall we implement any restriction so that cell merging will not be apply on Remarks field column. – himanshu Apr 24 '13 at 05:36
  • Yes, you can, you have to just know the appropriate index on which you want to limit it and has to program on that apropriatly. – Freelancer Apr 24 '13 at 05:40
  • cellCount is giving you index in my exaple, you have to mold it in your case to make restrictions – Freelancer Apr 24 '13 at 05:40
  • 1
    @Freelancer your sample works great with boundfields but I have templates defined in my gridview and this code is not working with it. Can you please help me to do the same with template fields. – Nikhil Gaur May 03 '13 at 08:43
  • 3
    @Nikhil may be this will be helpful http://stackoverflow.com/questions/13177139/how-to-use-rowspan-in-gridview-for-1st-column-only – Freelancer May 03 '13 at 08:46
  • I am assuming you meant `onRowDataBound` here? any way, it worked, after a few other similar codes. – AceMark Nov 18 '13 at 06:10
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;
            }
            else
                RowSpan = 2;
        }
    }

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

Shridhar Gowda
  • 59
  • 2
  • 10