2017-06-13 71 views
2

我试着打开DataGridView的双缓冲,但性能仍然差。没有附加CellFormatting事件,我使用了大约5%的CPU,但使用它,我的CPU使用了16% - 20%。随着双缓冲打开它接近25%。C#DataGridView CellFormatting

是否有替代方案可以用来更改单元格背景的颜色?

private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e) 
{ 
     { 
      string s = (String)e.Value; 
      s = s.Replace(" ",string.Empty); 
      if (s != string.Empty && s.Length > 0) 
      { 
       GUIRow r = gui[e.RowIndex]; 
       DataGridViewCell cell; 

       if(r.imLastBid){//.getSide() == domForm2.BID){ 

         cell = dataGridView1[1, e.RowIndex]; 
         cell.Style.BackColor = System.Drawing.Color.Salmon; 
        if(r.count){ 
         cell = dataGridView1[2, e.RowIndex]; 
         cell.Style.BackColor = System.Drawing.Color.Salmon; 
        }else{ 
         cell = dataGridView1[2, e.RowIndex]; 
         cell.Style.BackColor = System.Drawing.Color.OrangeRed; 
        } 

       }else if(r.imLastAsk){ 
         cell = dataGridView1[1, e.RowIndex]; 
         cell.Style.BackColor = System.Drawing.Color.DarkSeaGreen; 
        if(r.count){ 
         cell = dataGridView1[2, e.RowIndex]; 
         cell.Style.BackColor = System.Drawing.Color.DarkSeaGreen; 
        }else{ 
         cell = dataGridView1[2, e.RowIndex]; 
         cell.Style.BackColor = System.Drawing.Color.SeaGreen; 
        } 



       } 
       else{ 
        cell = dataGridView1[2, e.RowIndex]; 
        cell.Style.BackColor = System.Drawing.Color.White; 
        cell = dataGridView1[1, e.RowIndex]; 
        cell.Style.BackColor = System.Drawing.Color.White; 
       } 

       if(r.imLastPrice){ 
        cell = dataGridView1[0, e.RowIndex]; 
        cell.Style.BackColor = System.Drawing.Color.Yellow; 

       }else{ 
        cell = dataGridView1[0, e.RowIndex]; 
        cell.Style.BackColor = System.Drawing.Color.White; 
       } 


      } 
      else{ 
       DataGridViewCell cell; 
       cell = dataGridView1[1, e.RowIndex]; 
       cell.Style.BackColor = System.Drawing.Color.White; 
       cell = dataGridView1[2, e.RowIndex]; 
       cell.Style.BackColor = System.Drawing.Color.White; 
      } 

     } 
    } 
+0

我的解决方案是分离CellFormattingEvent,并在我进行GUI更新时手动调用上述逻辑。 CPU使用率下降到大约3 - 4% –

回答

1

您可以优化您的一些逻辑。考虑以下代码:

string s = (String)e.Value; 
s = s.Replace(" ",string.Empty); // <--- creates a new string, uh oh 
if (s != string.Empty && s.Length > 0) 

您正在为每个绘画事件创建一个新字符串。对于GUI事件来说这是一个昂贵的操作,所以尽可能避免内存分配。相反,使用这样的:

string s = (String)e.Value; 
if (!String.IsNullOrWhiteSpace(s)) 

这是怎么IsNullOrWhiteSpace实现:

if (value == null) 
{ 
    return true; 
} 
for (int i = 0; i < value.Length; i++) 
{ 
    if (!char.IsWhiteSpace(value[i])) 
    { 
     return false; 
    } 
} 
return true; 

正如你所看到的,它避免了创建一个新的字符串。

尝试单独进行此更改并查看您的性能是否提高。

+0

谢谢,这有助于我略微~2% –

0

您正在寻找优化单元格格式。我同意在那里做出优化,但我认为你会发现大多数性能问题来自网格填充时的渲染和重新渲染。出于性能方面的,我建议你看看其他几个常见的性能问题的DataGridView控件:

  1. 隐藏DataGridView的结合和/或重新填充,使其填充后再次可见之前。这通常会加快速度。

  2. 检查(并且优选地停止)的各种自动调整大小的属性或者设计时或数据绑定之前,for example

    dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing; //or even better .DisableResizing. Default is DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders 
    
  3. 仅加载那些立即使用Virtual Mode和寻呼所需的行。

+0

隐藏,但我的数据进来非常迅速,我每25ms更新GUI,因此它最终只是闪烁。 以下是一个示例:https://www.screencast.com/t/jYnQR4zf3a。我会考虑实施虚拟模式 –