How can I detect which mouse button was pressed in event CellClick, or how can I detect which cell was pressed in event MouseClick.
-
One question... is this really DataGridView? – varocarbas Nov 21 '13 at 09:05
-
Sorry it's RadGridView, my bad. – borkowij Nov 21 '13 at 09:07
-
No problem; I did misread your question too, so we are even :) – varocarbas Nov 21 '13 at 09:08
2 Answers
You can detect which cell was clicked by using Mouse Click event.
Then you have to cast sender to RadGridView, and then use CurrentCell property.
GridViewCellInfo dataCell = (sender as RadGridView).CurrentCell;
If you want to which mouse button was clicked use:
if (e.Button == MouseButtons.Right)
{
//your code here
}

- 145
- 1
- 9
-
This returns the selected cell, which may not be the one that was right clicked on. – Damien Apr 24 '23 at 10:48
I have written this answer thinking that you meant DataGridView
; but this code might also be useful for RadGridView
. What I usually do in these cases (with DataGridView
) is relying on a global flag to coordinate two different events; just a few global flags should be OK. Sample code:
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;
}
NOTE: the proposed global-variable-based approach is NOT the ideal proceeding, but a practical solution pretty handy in quite a few DataGridView-related situations. If there is a direct solution, as in this case (as proposed in the other answer or, in DataGridView, the CellMouseClick
event), you shouldn't ever use such an approach. I will let this answer as a reference anyway (for people looking for equivalent two-event-coordination situations, where no direct solution is present).

- 12,354
- 4
- 26
- 37
-
-
@bonio As said, not too much experience in RadGridView; but, at least in DataGridView, this is "required" (= the most adequate proceeding) in quite a few scenarios. – varocarbas Nov 21 '13 at 09:43