I need to format groups of rows separate from other rows depending on a cell
value. I'm sure others have done this as well.
For example, let's say I have 6 rows with a property called 'duration' with
the values:
2
1
5
5
5
7
I want to change the background color of the rows that have the value 5 to a
different color.
I've checked out a couple of the grid's events, but I'm not seeing anything
that sounds correct for this task. Something like "BindingRow".
I suupose that I would need to catch an event for each row's painting or
binding and then get the datasource for the row and then get the property
I'm interested in checking, but I'm not clear how to do this.
Have any of you done this?
I tried this code and ended up with a really funny result:
<code>
int sectionColorIndex = 0;
Color[] sectionColors = new Color[]
{
Color.Gainsboro,
Color.LightSalmon,
Color.LightSeaGreen,
Color.LightSkyBlue,
Color.LightCoral
};
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].Name ==
"DataGridViewComboBoxColumn_OpCode")
{
if (e.Value != null)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor =
sectionColors[sectionColorIndex];
if ((string)e.Value == "DJMPNZSKIP")
{
sectionColorIndex++;
if (sectionColorIndex 4)
{
sectionColorIndex = 0;
}
Debug.Assert(sectionColorIndex < sectionColors.Length,
"Not enough colors to format grid");
}
}
}
}
</code>
it seems that it's constantly calling format on the cells which results in
an animated DateGrid of my 5 colors. It's funny, but frustrating. I now
know I'm doing this wrong, please help before I vomoit from the colors!!
:0)