Search code examples
c#winformsdatagridviewdatagridviewcomboboxcell

Open dropdown(in a datagrid view) items on a single click


How can i avoid the double click on a DropDownButton used within a DataGridView? Right now I am able to view the drop down items within the DataGridView by clicking two or more times. First time it selects the cell and second time when I click on the DropDownButton arrow, it shows the list. How can I achieve the same in a single click?


Solution

  • You can achieve this by subscribing for the EditingControlShowing event of the grid and there for control of type ComboBox

    ComboBox ctl = e.Control as ComboBox;
    ctl.Enter -= new EventHandler(ctl_Enter);
    ctl.Enter += new EventHandler(ctl_Enter);
    

    And in the Enter event, use the property

    void ctl_Enter(object sender, EventArgs e)
    {
        (sender as ComboBox).DroppedDown = true;
    }
    

    DroppedDown indicates as the name suggests whether the dropdown area is shown or not, so whenever the control is entered this will set it to true and display the items without the need of further clicks.