如何在 CellClick 事件中检测哪个鼠标按键被按下,或者如何在 MouseClick 事件中检测哪个单元格被按下。
您可以通过使用鼠标点击事件来检测点击的单元格。
然后,您需要将发送器强制转换为RadGridView,然后使用CurrentCell属性。
GridViewCellInfo dataCell = (sender as RadGridView).CurrentCell;
如果您想知道点击了哪个鼠标按钮,请使用以下代码:
if (e.Button == MouseButtons.Right)
{
//your code here
}
DataGridView
;但是这段代码也可以用在RadGridView
上。在这种情况下(使用DataGridView
),我通常会依靠一个全局标志来协调两个不同的事件;只需要几个全局标志就可以了。示例代码:bool aCellWasSelected = false;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
aCellWasSelected = true;
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
DataGridViewCell selectedCell = null;
if (aCellWasSelected)
{
selectedCell = dataGridView1.SelectedCells[0];
MouseButtons curButton = e.Button;
//Do stuff with the given cell + button
}
aCellWasSelected = false;
}
注意:提出的基于全局变量的方法并不是理想的解决方案,但在许多DataGridView相关情况下非常实用。如果有直接的解决方案,比如在这种情况下(如其他答案中提出的或者在DataGridView中使用CellMouseClick
事件),就不应该使用这种方法。无论如何,我将保留此答案作为参考(供寻找等效的两个事件协调情况的人参考,在没有直接解决方案的情况下)。