我在 DataGridView 中绘制行,如下所示:
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;
}
}
}
然后我在 OnLoad 方法中调用它:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
AdjustColors();
}
我更喜欢 OnLoad 而不是 OnPaint 之类的东西..因为 OnPaint 被经常调用。
问题:为什么更改每一行的背景需要大约 100 - 200 毫秒?
早期,我在做 CellPaint..但是当我滚动刷新时遇到了问题..
而不是改变整体的颜色DataGrid
立即,您应该让它通过覆盖来管理渲染CellFormatting
事件。仅当行实际显示在屏幕上时才会绘制行。
private void aufgabenDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = aufgabenDataGridView.Rows[e.RowIndex];
AufgabeStatus status = (AufgabeStatus) Enum.Parse(typeof(AufgabeStatus), (string) row.Cells["StatusColumn"].Value);
switch (status)
{
case (AufgabeStatus.NotStarted):
e.CellStyle.BackColor = Color.LightCyan;
break;
case (AufgabeStatus.InProgress):
e.CellStyle.BackColor = Color.LemonChiffon;
break;
case (AufgabeStatus.Completed):
e.CellStyle.BackColor = Color.PaleGreen;
break;
case (AufgabeStatus.Deferred):
e.CellStyle.BackColor = Color.LightPink;
break;
default:
e.CellStyle.BackColor = Color.White;
break;
}
}
如果这仍然太慢,请尝试获取该行绑定到的真实对象:
...
DataGridViewRow row = aufgabenDataGridView.Rows[e.RowIndex];
var aufgabe = (Aufgabe) row.DataBoundItem;
AufgabeStatus status = aufgabe.Status;
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)