C#WinForms DataGridView背景颜色渲染太慢 [英] C# WinForms DataGridView background color rendering too slow

查看:1085
本文介绍了C#WinForms DataGridView背景颜色渲染太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private void AdjustColors()
{
foreach(augrabenDataGridView.Rows中的DataGridViewRow行)
{
AufgabeStatus status =(AufgabeStatus)Enum.Parse(typeof(AufgabeStatus),(string)row.Cells [StatusColumn]。 ;

开关(状态)
{
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;
(AufgabeStatus.Deferred):
row.DefaultCellStyle.BackColor = Color.LightPink;
break;
默认值:
row.DefaultCellStyle.BackColor = Color.White;
break;
}
}
}

然后我在OnLoad方法:

  protected override void OnLoad(EventArgs e)
{
base.OnLoad(e) ;

AdjustColors();
}

我喜欢OnLoad到OnPaint或某些东西..因为OnPaint是非常经常打电话的。



问题:为什么要改变每一行的背景大约100 - 200 ms?
早期,我是doint 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]。

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;
默认值:
e.CellStyle.BackColor = Color.White;
break;
}

}

如果这还是太慢,尝试获取该行所绑定的真实对象:

  ... 
DataGridViewRow row = aufgabenDataGridView.Rows [e .RowIndex];
var aufgabe =(Aufgabe)row.DataBoundItem;
AufgabeStatus status = aufgabe.Status;
...


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 DataGrid at once, you should let it manage the rendering by overriding the CellFormatting event. The rows will only be painted when they are actually displayed on the screen.

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;
  }

}

If this is still too slow, try getting the real object the row is bound to:

...
DataGridViewRow row = aufgabenDataGridView.Rows[e.RowIndex];
var aufgabe = (Aufgabe) row.DataBoundItem;
AufgabeStatus status = aufgabe.Status;
...

这篇关于C#WinForms DataGridView背景颜色渲染太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆