C# datagridview单元格鼠标悬停背景色更改

C# datagridview单元格鼠标悬停背景色更改,c#,.net,c#-4.0,datagridview,mouseover,C#,.net,C# 4.0,Datagridview,Mouseover,我想在datagridview中更改单元格的背景色,同时将鼠标悬停在特定单元格上 试用代码: private void dataGridView_whateventwillcomehere(object sender, DataGridViewCellEventArgs e) { } 在CellMouseMove事件上尝试此操作 private void dataGridView1_CellMouseMove(object sender, DataGridVi

我想在datagridview中更改单元格的背景色,同时将鼠标悬停在特定单元格上

试用代码:

private void dataGridView_whateventwillcomehere(object sender, DataGridViewCellEventArgs e)
        {

        }

CellMouseMove
事件上尝试此操作

private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Blue;
}
您需要
CellMouseLeave
事件来恢复颜色

private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
}

对于特定单元格,您需要提及
列名
,而不是
e.ColumnIndex
。此外,在DGV构造函数中,您需要设置双缓冲绘制,否则更改单元格样式会导致鼠标在DGV上移动时闪烁
this.SetStyle(ControlStyles.OptimizedDubleBuffer | ControlStyles.AllPaintingWimPaint,true);