动态改变DataGridView单元格颜色

38

我有一个dataGridView对象,其中填充了数据。我想点击一个按钮并将其更改单元格背景颜色。这是我目前拥有的:

foreach(DataGridViewRow row in dataGridView1.Rows)
{
    foreach(DataGridViewColumn col in dataGridView1.Columns)
    {
            //row.Cells[col.Index].Style.BackColor = Color.Green; //doesn't work
            //col.Cells[row.Index].Style.BackColor = Color.Green; //doesn't work
        dataGridView1[col.Index, row.Index].Style.BackColor = Color.Green; //doesn't work
    }
} 

这三种情况会导致表格以重叠的方式重新绘制,尝试调整表格大小会变得混乱。当单击单元格时,值仍然高亮显示,背景颜色不会改变。

问:如何在表格存在后更改单个单元格的背景颜色?

5个回答

95

这对我有效

dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;

@t4thilina,很高兴能帮助到你。干杯 :) - Ehsan
5
如果你尝试在表单构造函数中实现这个功能,它是不起作用的。然而,在OnLoad覆盖方法中对我来说是有效的。 - Robert H.
我必须在之后调用 dataGridView1.Refresh() 才能改变颜色。这个重置行排序的颜色让我开始搜索... - n00dles

5

实现自己的DataGridViewTextBoxCell扩展,并像这样重写Paint方法:

class MyDataGridViewTextBoxCell : DataGridViewTextBoxCell
{
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates cellState, object value, object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        if (value != null)
        {
            if ((bool) value)
            {
                cellStyle.BackColor = Color.LightGreen;
            }
            else
            {
                cellStyle.BackColor = Color.OrangeRed;
            }
        }
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
            formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}

然后在代码中将列的CellTemplate属性设置为您类的实例

columns.Add(new DataGridViewTextBoxColumn() {CellTemplate = new MyDataGridViewTextBoxCell()});

哈哈,我之前在为“警告”和“确定”选择最佳颜色,最终我选择了Color.OrangeRed作为“警告”的颜色,而选择了Color.SpringGreen作为“确定”的颜色。 - n00dles
您还可以通过 this.YourColumn.CellTemplatethis.dataGridView.Columns["YourName"].CellTemplate 来设置列的 CellTemplate。 - rémy

1
感谢,它正在运行。
在这里,我已经完成了,但是如果qty字段为零,则会显示红色单元格。
        int count = 0;

        foreach (DataGridViewRow row in ItemDg.Rows)
        {
            int qtyEntered = Convert.ToInt16(row.Cells[1].Value);
            if (qtyEntered <= 0)
            {
                ItemDg[0, count].Style.BackColor = Color.Red;//to color the row
                ItemDg[1, count].Style.BackColor = Color.Red;

                ItemDg[0, count].ReadOnly = true;//qty should not be enter for 0 inventory                       
            }
            ItemDg[0, count].Value = "0";//assign a default value to quantity enter
            count++;
        }

    }

1
如果您想让网格中的每个单元格具有相同的背景颜色,则可以执行以下操作:
dataGridView1.DefaultCellStyle.BackColor = Color.Green;

0
考虑使用DataBindingComplete事件来更新样式。下面的代码更改单元格的样式:
    private void Grid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        this.Grid.Rows[2].Cells[1].Style.BackColor = Color.Green;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接