问题
I am looking to merge cells across a set number of columns where the id in column 0 is the same.
An example of the DataGridView before Merged cells is:
The output I am looking for is:
In this example I am looking to merger cells across columns 0,1 and 2 where the Id is the same.
I have been looking to utilise code found at the following Code Example but cannot get it to produce the results I require.
Any assistance would be appreciated.
Update
I managed to get a working version but the down side is every time I scroll the DataGridView witha much larger dataset than the examples above it repaints the cells as a blend between the original and required output.
My code is
int _rowIndex = 0;
public bool IsColumnZeroSameValue(int column, int row)
{
if (column > 0)
{
return false;
}
if (dgvData[column, row].Value.ToString() == dgvData[column, row - 1].Value.ToString())
{
_rowIndex = row;
return true;
}
return false;
}
private void dgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == 0)
return;
IsColumnZeroSameValue(e.ColumnIndex, e.RowIndex);
if (e.RowIndex ==_rowIndex & e.ColumnIndex<2)
{
e.Value = "";
e.FormattingApplied = true;
}
}
private void dgvData_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.RowIndex < 1 || e.ColumnIndex < 0)
return;
if (e.RowIndex == _rowIndex & e.ColumnIndex < 2)
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top = dgvData.AdvancedCellBorderStyle.Top;
}
}
Any ideas on how to resolve this issue would be much appreciated.
来源:https://stackoverflow.com/questions/65355319/how-to-merge-datagridview-cells-based-on-same-id-column-0