views:

52

answers:

2

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

Ex.

| Person 1 | Person 2 | Person 3 |

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

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

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

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.

Do the DataGridView have any event that would be usefull?

+1  A: 

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

Bharath K
+4  A: 

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

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.

Gary
CellFormatting is an event so you don't have to subclass, you can simply subscribe to the event. Both achive the same thing.
Tergiver
Thx, the OnCellFormatting event is looking promising. Just did a small test private void prisListeView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.Value != null) if (e.ColumnIndex == 3 } }But how do I change the style for the other cells? Ex. In the above Column 3 have changed. How do I then change the style for the other cells?
gulbaek
Not sure about the performance in this solution, but it works :-)And if someone needs to know how to change the other cells styles, here is the codedatagridview1.Rows[e.RowIndex].Cells["myColumn"].Style.BackColor = Color.DeepPink;
gulbaek