I’m painting my rows in a DataGridView like this:
private void AdjustColors()
{
foreach (DataGridViewRow row in aufgabenDataGridView.Rows)
{
AufgabeStatus status = (AufgabeStatus)Enum.Parse(typeof(AufgabeStatus), (string)row.Cells["StatusColumn"].Value);
switch (status)
{
case (AufgabeStatus.NotStarted):
row.DefaultCellStyle.BackColor = Color.LightCyan;
break;
case (AufgabeStatus.InProgress):
row.DefaultCellStyle.BackColor = Color.LemonChiffon;
break;
case (AufgabeStatus.Completed):
row.DefaultCellStyle.BackColor = Color.PaleGreen;
break;
case (AufgabeStatus.Deferred):
row.DefaultCellStyle.BackColor = Color.LightPink;
break;
default:
row.DefaultCellStyle.BackColor = Color.White;
break;
}
}
}
Then I call it in the OnLoad method:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
AdjustColors();
}
I prefer OnLoad to OnPaint or something.. because OnPaint is called very often.
The question: Why does it take about 100 – 200 ms to change the background of every row?
Early, I was doint CellPaint.. but I had problems when scrolling with refreshing..
Instead of changing the color of the whole
DataGridat once, you should let it manage the rendering by overriding theCellFormattingevent. The rows will only be painted when they are actually displayed on the screen.If this is still too slow, try getting the real object the row is bound to: