2017-06-13 26 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。我會考慮實施虛擬模式 –