I have DataGridView
on my form application. After retrieving data from a table in the database and displaying them in DataGridView
, I apply a green color to some cell's BackColor of the rows if a certain condition is met.
After those cells are colored green, the program makes them go through another condition, which colors the whole row's BackColor red if they fail to satisfy the condition.
However, it seems like pre-colored cells cannot be overwritten with a new color. Even if I apply the following code to color the whole row red, it only works for the cells that have not been pre-colored.
for(int i=0; i<myDataGridview.Rows.Count; i++){
if(/*a certain condition FAILS*/){
myDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
}
Right now, I am coloring those pre-colored cells red one by one, but this takes a lot of time and code like:
for(int i=0; i<myDataGridview.Rows.Count; i++){
if(/*a certain condition FAILS*/){
//Trying to color the whole row RED, but not working
myDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Red;
//Manually color the cells, which are pre-colored to green, RED
myDataGridView.Rows[i].Cells[6].Style.BackColor = Color.Red;
myDataGridView.Rows[i].Cells[7].Style.BackColor = Color.Red;
....
myDataGridView.Rows[i].Cells[13].Style.BackColor = Color.Red;
myDataGridView.Rows[i].Cells[16].Style.BackColor = Color.Red;
}
}
I am wondering if there is a better way to override the backColor. Can someone please help?
Here is an example (imitation) DataGridView.
Those who failed the first condition automatically get their whole row red, and that works. However, if they pass the first condition and get their "Passed1" cell colored green, and then fail the second condition, as you can see, those cells stay green. I want to color the whole row red, even overwriting the pre-colored-to-green cell to red.
CellPainting
event. If you code for the full set of conditions it should suffice. – TaW