I have a table an some values on it. If the Row Cell "Name" is not empty change color of background to violet.
Name ID Customers
Niky 1 yes // here change background to violet
2 no
Donna 3 yes // here change background to violet
Baka 4 no // here change background to violet
5 yes
6 no
I have tried this code but i doesnt work, dont know why:
foreach (DataGridViewRow row1 in dataGridView1.Rows)
{
if (row1.Cells[0].Value != null)
{
row1.DefaultCellStyle.BackColor = Color.Violet;
}
}
you can do it on the rowdatabound event of the gridview
You can also use this use can go throught it:
The usual place to put this sort if code in within the
DataBindingComplete
event handler, either attach the event as below or using the designer:Then in the handler you have something like this:
In the code above I've changed your original code to now looks at all cells rather than just the first.
You can also put the code in the CellFormatting event.
I dont know where you put your colour code but Ive always done it in the drawing section
Heres oen where I coloured the line depending on a status, except for the first column which was dependant on yellow or blue - this is work in progress code and should be tidied up
Or you can do:
so if all the first cell of a line is not empty, colour the row violet.
A good (but not performant) way for achieving this is the event
cell-formatting
of the datagridview. You can read the whole documentation in the MSDN. The essence is, that you subscribe to the formatting-event and in this you can do your formatting-issues.This way you can be sure that the colors etc. are just fine no matter if the user is resizing, scrolling whatever.
Btw: I wouldn't do it in the drawing-event as I learned that usually you will break something of the automated drawing-logic ;)
For further information there is an article about cellstyles in the MSDN which will be in this case overkill. But you can never know enought :)