问题
How can I detect which mouse button was pressed in event CellClick, or how can I detect which cell was pressed in event MouseClick.
回答1:
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
}
回答2:
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).
来源:https://stackoverflow.com/questions/20116095/radgridview-detect-cellclick-event-button