C#WinForms DataGridView背景色渲染太慢
我正在DataGridView中绘制行,如下所示:C#WinForms DataGridView背景色渲染太慢,c#,winforms,datagridview,C#,Winforms,Datagridview,我正在DataGridView中绘制行,如下所示: private void AdjustColors() { foreach (DataGridViewRow row in aufgabenDataGridView.Rows) { AufgabeStatus status = (AufgabeStatus)Enum.Parse(typeof(AufgabeStatus), (string)row.Cells
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();
}
我更喜欢上装,而不是上漆什么的。。因为OnPaint经常被调用
问题:为什么改变每行的背景需要100-200毫秒?
早些时候,我在画画。。但是我在滚动刷新时遇到问题。可能是Enum.Parse调用,它的性能很差。您应该尝试将其更改为字典查找,以查看这是否会提高性能。正如SwDevMan1所说,您应该首先删除Enum.Parse调用。您是否使用数据绑定来填充网格?如果是,则可以使用Rows[index].DataBoundItem访问该行的数据绑定对象,并直接访问AufgabeStatus状态
我建议的第二个调整是在操纵网格之前和之后分别调用SuspendLayout()和ResumeLayout()。与其立即更改整个
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;
...
如果属性与预期值不同,则最好只设置属性。这样就不会触发不必要的内部DataGridView开销 如果一行中的所有单元格的格式都相同,则可以在行级别而不是单元格级别进行格式设置
DataGridViewCellStyle rowStyle = row.DefaultCellStyle;
if (rowStyle.BackColor != status.BackColor) {
rowStyle.BackColor = status.BackColor;
}
不要尝试行格式
作为row.defaultcellstyle
请尝试在中设置单个单元格格式
ufgabenDataGridView\u单元格式
单元格[0]。style.backcolor=color.yellow您的意思是每行需要100-200毫秒?听起来很重。有几排?表单上是否设置了双缓冲区?你试过使用CellFormatting事件吗?我删除了解析调用。是的,我正在使用数据绑定。我直接通过DataBoundItem访问元素。我尝试暂停并恢复。更快。。但在大约400毫秒内,15行仍然是彩色的。这可能证明我自己无法处理绘画,但我应该依靠女士提供的机制。现在所有的行都立即重新着色!我删除了ShDevMan+1建议的Enum.Parsing,谢谢……我总是忘记在哪个事件中执行此操作。这将为每个单元格运行,对吗?我希望有一个每行只触发一次的事件?如果要更改单个单元格的颜色,请测试引发该事件的单元格:如果(e.ColumnIndex==piecePartMasterGridview.Columns[“AufgabeStatus”].Index)正确,则Enum.Parse足够慢。我宁愿直接访问DataBoundItem。