我試着打開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;
}
}
}
我的解決方案是分離CellFormattingEvent,並在我進行GUI更新時手動調用上述邏輯。 CPU使用率下降到大約3 - 4% –