我试着打开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% –