I have a datagridview made up of multiple rows and columns. I want to iterate through each row and check the contents of a specific column. If that column contains the word "NO", I want to change the forecolor of the entire row to Red. Here is an attempt at some code so far but It's certainly not working, starting to wonder If I need to iterate over every cell?
CODE:
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No"))
{
dgvr.DefaultCellStyle.ForeColor = Color.Red;
}
}
hook up OnRowDataBound event then do stuff
ASPX (Grid):
Code Behind:
FOR WinForms:
Is it possible there are spaces or some other character as part of the cell value? If so try using the Contains method rather than straight equality.
This code works fine for me:
Other than casting as a string rather than calling ToString I dont really see any difference so it could be a case sensitivity bug. Try using:
On your DataGridView, handle the CellFormatting event:
Your event handler could then look like this:
In this way you aren't 'iterating' over the rows -- simply changing the color with which they are painted/drawn when they become visible (and thus require formatting) in the grid.
This is the solution for Winforms: