Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help how to merge rows in datagridview in c# dynamically based on the records retrieved from database.

What I have tried:

This is what i have tried but in vain.
C#
private void MergeCellsInColumn(int col, int row1, int row2)
     {
         Graphics g = dgv_loadsurvey.CreateGraphics();
         Pen p = new Pen(dgv_loadsurvey.GridColor);
         System.Drawing.Rectangle r1 = dgv_loadsurvey.GetCellDisplayRectangle(col, row1, true);
         System.Drawing.Rectangle r2 = dgv_loadsurvey.GetCellDisplayRectangle(col, row2, true);

         int recHeight = 0;
         string recValue = string.Empty;
         for (int i = row1; i <= row2; i++)
         {
             recHeight += dgv_loadsurvey.GetCellDisplayRectangle(col, i, true).Height;
             if (dgv_loadsurvey[col, i].Value != null)
                 recValue += dgv_loadsurvey[col, i].Value.ToString() + " ";
         }
         System.Drawing.Rectangle newCell = new System.Drawing.Rectangle(r1.X, r1.Y, r1.Width, recHeight);
         g.FillRectangle(new SolidBrush(dgv_loadsurvey.DefaultCellStyle.BackColor), newCell);
         g.DrawRectangle(p, newCell);
         g.DrawString(recValue, dgv_loadsurvey.DefaultCellStyle.Font, new SolidBrush(dgv_loadsurvey.DefaultCellStyle.ForeColor), newCell.X + 3, newCell.Y + 3);
     }
Posted
Updated 9-Jun-16 2:50am
v2
Comments
CHill60 9-Jun-16 8:45am    
What do you mean by "in vain" - what is the problem? You also need to give a proper description of what you are trying to do ... merge cells or merge rows?
User1454 9-Jun-16 9:02am    
I need to merge rows of index from 0 to 47 of column index 1. But datagridview is not merging those rows.

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900