Carl Shiles
Carl Shiles

Reputation: 444

How to Differentiate DataGridViewComboBoxColumn Cell Click Events

I have a DataGridViewComboBoxColumn that triggers an event on cell click, which ends up displaying a Dialog Box. This works great, but my problem is that I want this to only trigger if the user did NOT click on the arrow to display the dropdown. Right now there is no differentiation between a click on the text & on this arrow. How do I make this distinction? (I am aware of the CellContentClick event, but this can take an obnoxious number of clicks to actually trigger.)

enter image description here

(In my amazing illustration, I want green to trigger the event, and red just have its normal functionality.)

Upvotes: 1

Views: 219

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

To detect if the mouse has been clicked on the dropdown button area in DataGridViewComboBoxCell, you can use the following code:

private void productsDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var field = typeof(DataGridViewComboBoxCell).GetField("mouseInDropDownButtonBounds",
        System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
    var mouseInDropDownButtonBounds = field.GetValue(null);
}

You can take a look at source code of DataGridViewComboBoxCell to find out more about how it calculates the dropdown button bound.

Upvotes: 1

You can use the EditingControlShowing event:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e){
    if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)){
        //The green is clicked

   }
}

Upvotes: 0

Related Questions