更改单元格背景色后 DataGridView 不刷新

时间:2021-01-04 16:19:11

标签: c# datagridview

我正在从列表中填充 DataGridView。在其中一列中,我需要能够使用 ColorDialog 更改单个单元格的 BackColor。

我这样做:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (e.ColumnIndex == 2) {
        ColorDialog cd1 = new ColorDialog();
        if (cd1.ShowDialog() == DialogResult.OK) {
            Color color = cd1.Color;
            dataGridView1.Rows[e.RowIndex].Cells[2].Style.BackColor = color;
        }
    }
}

但是,BackColor 不会立即出现,而是在鼠标单击 DataGrid 后才会出现。

我尝试过:

dataGridView1.Update();
dataGridView1.Refresh();

似乎没有任何效果。

有没有办法在颜色改变后自动刷新单元格?

1 个答案:

答案 0 :(得分:1)

像这样更改单元格背景色后,尝试更改当前单元格。

if (e.ColumnIndex == 1)
{
    ColorDialog colorDialog = new ColorDialog();
    if (colorDialog.ShowDialog() == DialogResult.OK)
    {
        Color color = colorDialog.Color;
        dgvExample.CurrentCell.Style.BackColor = color;
        dgvExample.CurrentCell = dgvExample.CurrentRow.Cells[0];
    }
}