如果具有相同的值错误,则合并DataGridView单元格

时间:2017-05-03 08:35:17

标签: c# datagridview

我想合并DataGridView单元格,如果它们具有相同的值。一切看起来都不错,但如果我点击超过2个复选框。它显示错误,如下图所示: enter image description here

 private void grdDNSELECT_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        try
        {
            e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
            if (e.RowIndex < 1 || e.ColumnIndex < 0)
                return;
            e.AdvancedBorderStyle.Top = IsTheSameCellValue(e.ColumnIndex, e.RowIndex) ? DataGridViewAdvancedCellBorderStyle.None : grdDNSELECT.AdvancedCellBorderStyle.Top;
        }
        catch (Exception ex)
        {

            //throw ex;
        }
    }

    private void grdDNSELECT_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        try
        {
            if (e.RowIndex == 0)
                return;
            if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
            {

                e.Value = "";
                e.FormattingApplied = true;
            }
        }
        finally
        {
           //
        }
    }

1 个答案:

答案 0 :(得分:0)

我通过添加条件解决了这个问题:

private void grdDNSELECT_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    try
    {
        if (e.RowIndex == 0|| e.ColumnIndex==0)
            return;
        if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
        {

            e.Value = "";
            e.FormattingApplied = true;
        }
    }
    finally
    {
       //
    }
}