RadGridView检测CellClick事件按钮

如何检测事件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事件),您不应该使用这种方法。 无论如何,我将把这个答案作为参考(对于那些寻找等效的双事件协调情况,没有直接解决方案的人)。