Search code examples
c#winformsdatagridviewdatagridviewcomboboxcell

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


Solution

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