Update DatagridView cell background color based on row data

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

Ex.

| Man 1 | Man 2 | Face 3 |

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

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

In the first line, I would like β€œ100” to have a red background color and β€œ200” green. Or. Lowest value = red maximum = green

Now the thing is, Im using a BindingList for my data and updating it async with INotifyPropertyChanged. Therefore, I need to check every time every time one of the values ​​has been updated.

Is there an event in the DataGridView that would be useful?

+6
c # winforms datagridview
source share
2 answers

Subclass DataGridView and override OnCellFormating (), here you can check the value of the cell and adjust the colors correctly.

Regarding the comments below about using the corresponding event, yes, you can do it, but the virtual method has better performance and subclass. DataGridView encapsulates all the behavior of your grid in one place.

+9
source share

Not too sure if this is the best solution, but hope this helps: Register for the PropertyChanged event for each data source element in the datagrid view. In the event handler, you can perform the required action based on the changed values.
If you are using a binding list for your data source, register with ListChanged to process the new items.

+1
source share

All Articles