0

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:

enter image description here

The output I am looking for is:

enter image description here

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.

StephenH
  • 93
  • 1
  • 8
  • DGV is not the best tool for doing this. See this codeview project : https://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView-2 – jdweng Dec 18 '20 at 11:01
  • Thank you for the suggestion at first glance I cannot see how this will provide what i am looking for but I will look in more detail. However I do want to utilise DataGridView if at all possible. – StephenH Dec 18 '20 at 11:48

1 Answers1

0

You could modify IsColumnsZeroSameValue to validate based in a fixed column and not in the painting column, see the attached image.

Formating datagrid grouped rows