My code seems pretty straight forward, I'm not sure what I'm missing...
I'm looping through each row in dataGridView.Rows
, then looping through each cell and checking if its value is "OK"
then I change the cell color to Color.Red
and also write "IF WAS TRUE"
in console.
I have that nice little "IF WAS TRUE"
in console window just to prove to myself that the if statement is getting caught (it is). But my cell color remains unchanged White
.
Any advice would be appreciated!
public partial class Form1 : Form
{
public static BindingList<Record> record_list = new BindingList<Record> { };
public static DataGridViewCellStyle style = new DataGridViewCellStyle();
public Form1()
{
InitializeComponent();
FillData();
foreach (DataGridViewRow row in dataGridView.Rows)
{
for (var i = 0; i < row.Cells.Count; i++)
{
Console.WriteLine(row.Cells[i].Value);
if (row.Cells[i].Value as string == "OK")
{
row.Cells[i].Style.BackColor = Color.Red;
Console.WriteLine("IF WAS TRUE");
}
}
}
}
void FillData()
{
record_list.Add(new Record("Support-19", "TEST", 0, 0.0, "",
"LOC CODE2", 0.0, 0, 0, 0.0, ""));
record_list.Add(new Record("Support-99", "ROBOTS", 0, 0.0, "",
"OK", 0.0, 0, 0, 0.0, ""));
record_list.Add(new Record("Support-27", "TEST2", 0, 0.0, "",
"LOC CODE1", 0.0, 0, 0, 0.0, ""));
dataGridView.DataSource = record_list;
}
}
public class Record
{
public string Station { get; set; }
public string UserName { get; set; }
public int EvtActive { get; set; }
public double EvtTime { get; set; }
public string EvtTimeString { get; set; }
public string LocCode { get; set; }
public double LastLoop { get; set; }
public int CompLvl { get; set; }
public int RecordID { get; set; }
public double ConnectTime { get; set; }
public string Notes { get; set; }
public Record(string a, string b, int c, double d, string e,
string f, double g, int h, int i, double j, string k)
{
this.Station = a;
this.UserName = b;
this.EvtActive = c;
this.EvtTime = d;
this.EvtTimeString = e;
this.LocCode = f;
this.LastLoop = g;
this.CompLvl = h;
this.RecordID = i;
this.ConnectTime = j;
this.Notes = k;
}
}
EDIT: This was marked as duplicate, but this seems different to me. In the other SO post, it looks like a cell is being sent to a function directly where as I am iterating over cells. I try to call the BackColor property in a similar manner and I get no results...
See Question&Answers more detail:
os