根据行数据更新 DatagridView 单元格背景颜色 [英] Update DatagridView Cell Background color based on row data

查看:19
本文介绍了根据行数据更新 DatagridView 单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DatagridView,我希望它根据每一行中的数据改变背景颜色.

Hi I got a DatagridView and I would like it to change background color depending on the data in each row.

例如.

|人 1 |人 2 |第 3 个人 |

| Person 1 | Person 2 | Person 3 |

|----100----|---200-----|-----150----|

|----100----|---200-----|-----150----|

|----300----|---100-----|------50----|

|----300----|---100-----|------50----|

在第一行中,我希望它使100"具有红色背景色和200"绿色背景.或者.最低值 = 红色最高 = 绿色

In the first row I would like it to make "100" have a red background color and "200" green. Or. The lowest value = red highest = green

现在的问题是,我为我的数据使用了 BindingList,并且它正在与 INotifyPropertyChanged 异步更新.因此,每次更新其中一个值时,我都需要某种方式进行检查.

Now the thing is that Im using a BindingList for my data, and its being updated async with INotifyPropertyChanged. So I need some way of checking each time one of the values has been updated.

DataGridView 是否有任何有用的事件?

Do the DataGridView have any event that would be usefull?

推荐答案

Subclass DataGridView 并覆盖 OnCellFormating(),在这里您可以检查单元格值并适当设置颜色.

Subclass DataGridView and override OnCellFormating(), in here you can inspect the cell value and set the colours appropriately.

关于下面关于使用相应事件的评论,是的,您可以这样做,但虚拟方法具有更好的性能,并且子类化 DataGridView 将网格的所有行为封装在一个地方.

In regard to comments below about using the corresponding event, yes you can do that but the virtual method has better performance and subclassing DataGridView encapsulates all the behavior of your grid in one place.

这篇关于根据行数据更新 DatagridView 单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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