I have a function merge cell in datagridview in clr window form application as below:
void EquipmentDataScreen::MergeCellsInColumn(DataGridView^ datagrid,int col, int row1, int row2)
{
Graphics^ g = datagrid->CreateGraphics();
Pen^ p = gcnew Pen(datagrid->GridColor);
Rectangle r1 = datagrid->GetCellDisplayRectangle(col, row1, true);
Rectangle r2 = datagrid->GetCellDisplayRectangle(col, row2, true);
int recHeight = 0;
String^ recValue ="";
for (int i = row1; i <= row2; i++)
{
recHeight += datagrid->GetCellDisplayRectangle(col, i, true).Height;
if (datagrid->Rows[i]->Cells[col]->Value != nullptr)
recValue += datagrid->Rows[i]->Cells[col]->Value->ToString() + " ";
}
Rectangle newCell = Rectangle(r1.X, r1.Y, r1.Width-1, recHeight-1);
g->FillRectangle(gcnew SolidBrush(datagrid->DefaultCellStyle->BackColor), newCell);
g->DrawRectangle(p, newCell);
StringFormat^ strformat = gcnew StringFormat();
strformat->Alignment = StringAlignment::Center;
strformat->LineAlignment = StringAlignment::Center;
g->DrawString(recValue, datagrid->DefaultCellStyle->Font, gcnew SolidBrush(datagrid->DefaultCellStyle->ForeColor), newCell, strformat);
//g->DrawString(recValue, datagrid->DefaultCellStyle->Font, gcnew SolidBrush(datagrid->DefaultCellStyle->ForeColor), newCell.X + 3, newCell.Y + 3);
}
But this function simply draws a rectangle to cells that I want to merge. The question is "is there a way to actually merge cell in datagridview then add combobox or text box to cell was merged?" Please help me. Thank you very much!