I am probably missing something realy simple, but i cannot get it to work properly.
I am filling a Datagridview. Then i have a Databindingcomplete event to go over the rows and give them colors and fonts depending on certain criteria.
This is done with the following code:
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if ((r.Cells["referrer"].Value.ToString().Contains("DBS")) & (r.Cells["priority"].Value.ToString().Equals("1")))
{
r.DefaultCellStyle = DBS;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
}
else if ((r.Cells["referrer"].Value.ToString().Contains("DBS")) & (r.Cells["priority"].Value.ToString().Equals("0")))
{
r.DefaultCellStyle = DBS;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Regular);
}
else if ((r.Cells["referrer"].Value.ToString().Contains("agenda")) & (r.Cells["priority"].Value.ToString().Equals("0")))
{
r.DefaultCellStyle = agenda;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Regular);
}
else if ((r.Cells["referrer"].Value.ToString().Contains("agenda")) & (r.Cells["priority"].Value.ToString().Equals("1")))
{
r.DefaultCellStyle = agenda;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
}
else if ((r.Cells["referrer"].Value.ToString().Contains("sms")) & (r.Cells["priority"].Value.ToString().Equals("1")))
{
r.DefaultCellStyle = sms;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
}
else if ((r.Cells["referrer"].Value.ToString().Contains("sms")) & (r.Cells["priority"].Value.ToString().Equals("0")))
{
r.DefaultCellStyle = sms;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Regular);
}
else if ((r.Cells["referrer"].Value.ToString().Contains("remoteLogin")) & (r.Cells["priority"].Value.ToString().Equals("1")))
{
r.DefaultCellStyle = remoteLogin;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
}
else if ((r.Cells["referrer"].Value.ToString().Contains("remoteLogin")) & (r.Cells["priority"].Value.ToString().Equals("0")))
{
r.DefaultCellStyle = remoteLogin;
r.DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Regular);
}
}
Now this is working. the first line of the DefaultCellStyle is for colors of the row, then the second line is the font.
Everything works ok, colors are good, bold works, regular works, except one thing.
If i have 2 rows:
A) agenda with priority 1
B) agenda with priority 0
It will go past the first one, adjusts the color good, makes it bold. But then the second one will also be bold. If i change them so the first one is priority 0, both will be regular.
I can have all types of events, all will be bold or regular, all have the correct color settings. But when i have a second of of the same type (DBS, agenda, remoteLogin or sms) the second and so on will have the same font as the first one.
So this batch will do the following:
[DBS – 1] –> Correct color, and bold (correct)
[agenda – 0] –> Correct color and regular (correct)
[sms – 1] –> Correct color, and bold (correct)
[remoteLogin – 0] –> Correct color and regular (correct)
[DBS – 0] –> Correct color and Bold (Incorrect font!)
[agenda – 1] –> Correct color and regular (Incorrect font!)
I hope i make sense explaining. Maybe i’m not seeing something or i want something stupid. Maybe someone can tell me what is wrong.
the code might not be the best now, since i have made many adjustments trying to make it work.
Edit:
I just noticed that it does do the foreach correct, but when it encounters the a second one of the same type, it will change the font of the one before also. Maybe i can work around it.
Thx in advance!
Mike
I figured it out.
I create the DefaultCellStyle with the colors for each item (DBS, agenda, etc.) Then i change the Font of that DefaultCellStyle to Bold or regular. When the foreach loop encounters a second row of the same type. It loads the same DefaultCellStyle, lets say “DBS” as the other one. Inside of that i adjust the Fontstyle. Because i now update the Fontstyle inside the “DBS” item, all rows with the “DBS” DefaultCellStyle get affected. I think the only way around this is to make a diffrent DefaultCellStyle for each option in the list.