Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datagridview性能中的彩色单元格_C#_.net_Winforms - Fatal编程技术网

C# datagridview性能中的彩色单元格

C# datagridview性能中的彩色单元格,c#,.net,winforms,C#,.net,Winforms,我需要给DataGridView中的特定单元格着色。我试过了,但没用 dataGridView1.Rows[1].Cells[1].Style.BackColor = Color.Red; 但对于整个专栏来说,它是有效的 dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.Red; 然后我尝试了下面的代码,它确实起了作用,但是datagridview在2k行时的性能非常慢。是否有可能提高性能,或者以其他方式为datagrid

我需要给DataGridView中的特定单元格着色。我试过了,但没用

dataGridView1.Rows[1].Cells[1].Style.BackColor = Color.Red;
但对于整个专栏来说,它是有效的

dataGridView1.Columns[2].DefaultCellStyle.BackColor = Color.Red;
然后我尝试了下面的代码,它确实起了作用,但是datagridview在2k行时的性能非常慢。是否有可能提高性能,或者以其他方式为datagridview中的特定单元格着色。谢谢

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewCellStyle MakeItRed = new DataGridViewCellStyle();
    MakeItRed.BackColor = Color.Red;

    //make a whole column red
    dataGridView1.Columns[1].DefaultCellStyle = MakeItRed;

    //make a specific cell red
    DataGridViewRow row2 = dataGridView1.Rows[2];
    row2.Cells[2].Style = MakeItRed;
}

当用户同时只能看到~50行时,在网格中有2k行是否明智?除了性能问题现在变得明显之外,它一开始效率不高。顶部的一行(
dataGridView1.Rows[1]。Cells[1]。Style.BackColor=Color.Red;
)应该可以工作。这里有。你在哪里,什么时候,怎么称呼它的?(假设问题不在于列索引的变化..)是否调用了
dataGridView1.Rows[1]。Cells[1]。Style.BackColor=Color.Red
dataGridView1.Columns[2]。DefaultCellStyle.BackColor=Color.Red
表单中
构造函数?如果是这样,对行/单个单元格的更改将失败,因为
表单
数据网格视图
尚不可见。如果在以后的任何时候调用,就像在您的
DataGridView.CellFormatting
解决方案中一样,调用应该可以正常工作。尝试在
表单中调用它。加载