在我的C#datagridview中,我希望用户被确认他们确实已经单击了该单元格。DataGridView Mousedown和Mouseup单元格背景颜色变化不起作用
我正在使用datagridview的MouseDown和MouseUp事件。通过将单元格颜色更改为蓝色,代码可以正常运行MouseDown事件,但MouseUp事件不会将单元格的颜色更改为透明。
由此产生的功能是,我点击的所有单元格变为蓝色,并保持蓝色。
我没有正确调用Refresh方法吗?有没有更好的方法来实现相同的目标?
这里是我的代码:
private void Selector_dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.BackColor = Color.Blue;
Selector_dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;
Selector_dataGridView.Refresh();
}
private void Selector_dataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
CellStyle.BackColor = Color.Transparent;
Selector_dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;
Selector_dataGridView.Refresh();
}
这工作。它看起来像我不得不使用Color.White,我不能使用Color.Empty或Color.Transparent。谢谢! – TheBear