【问题标题】:DataGridView not refreshing after changing cell BackColor更改单元格 BackColor 后 DataGridView 不刷新
【发布时间】:2021-04-10 10:39:51
【问题描述】:

我正在从列表中填充 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();

似乎没有任何效果。

有什么方法可以让单元格在颜色改变后自动刷新?

【问题讨论】:

    标签: c# datagridview


    【解决方案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];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 2013-06-12
      • 2013-08-25
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多