4

How can I detect which mouse button was pressed in event CellClick, or how can I detect which cell was pressed in event MouseClick.

varocarbas
  • 12,354
  • 4
  • 26
  • 37
borkowij
  • 182
  • 1
  • 3
  • 11

2 Answers2

4

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
}
soshman
  • 145
  • 1
  • 9
1

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).

varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • Thanks, but I would prefer not to use global variables. – borkowij Nov 21 '13 at 09:38
  • @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