1

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!

HuyHoang
  • 11
  • 1
  • This is a link to merge cells. It will need converting to c++/cli but thats usually easy enough. https://stackoverflow.com/questions/16774966/how-to-merge-datagridview-cell-in-winforms . I have some code that creates a combobox, numeric up down or a date time picker when you click on a cell in a DataGirdView, is that what you mean when you say '... then add combobox or text box to cell was merged?' – ZedLepplin Aug 09 '19 at 08:06
  • I saw your link before I add this topic. Your mechanism merge is merge cell with same value. But after applied your way to merge cell, user still select to cell inside the cell was merged. Actually, I want merge many cells in row or column to one cell then we can add component (combobox, textbox) to new cell. You can visually as merge cell in excel file – HuyHoang Aug 09 '19 at 08:24
  • What about creating your own control: - https://learn.microsoft.com/en-us/visualstudio/data-tools/create-a-windows-forms-user-control-that-supports-complex-data-binding?view=vs-2019 . – ZedLepplin Aug 09 '19 at 08:39

0 Answers0