You can do this in multiple ways. It depends a bit on the exact needs and when the cell needs to change color.
Here is a solution that changes the color of the cell when it is loaded. If needed, it is also possible to change the color of the cell when other cell changes. Herefor you need to hook on a different event.
On the RowPrePaint event, add the following code:
private void dgSampleGrid_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (!e.IsLastVisibleRow)
{
DataGridViewRow row = dgSampleGrid.Rows[e.RowIndex];
if ((bool)row.Cells["boolColumn"].Value && (int)row.Cells["intColumn"].Value < 0)
{
row.Cells["intColumn"].Style.BackColor = Color.Red;
}
}
}
The e.IsLastVisibleRow check is done to avoid changing colors on the Edit row. It is possible to set this row off.
Greetz,
Geert
Geert Verhoeven
Consultant @ Ausy Belgium
My Personal Blog