动态更改 datagridview 单元格颜色 [英] Changing datagridview cell color dynamically

查看:51
本文介绍了动态更改 datagridview 单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个填充了数据的 dataGridView 对象.我想单击一个按钮并让它更改单元格背景的颜色.这是我目前拥有的

I have a dataGridView object that is populated with data. I want to click a button and have it change the color of the background of the cell. This is what I currently have

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
    }
} 

所有这三个都会导致表格以重叠的方式重新绘制在其自身上,并且尝试重新调整表格的大小变得一团糟.单击单元格时,该值保持突出显示并且背景颜色不会改变.

ALL of these three cause the table to be redrawn over itself in an overlapping manner and trying to re-size the tables becomes a mess. when clicking on a cell, the value remains highlighted and the backcolor doesn't change.

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

Q: How can I change the backcolor of an individual cell after the table exists?

推荐答案

这对我有用

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

这篇关于动态更改 datagridview 单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆