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.
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);
}