44

In winforms, you need to click the combobox twice to properly activate it - the first time to focus it, the second time to actually get the dropdown list.

How do I change this behavior so that it activates on the very first click?

This is for DATAGRIDVIEW combobox.

philu
  • 795
  • 1
  • 8
  • 17
TtT23
  • 6,876
  • 34
  • 103
  • 174
  • If you directly click the drop down arrow ,it will get activated.Right? – Rockstart Oct 22 '12 at 04:56
  • @Rockstart no, it has to be first focused, and then you can activate it on the second try – TtT23 Oct 22 '12 at 05:22
  • Go with this [Link](http://stackoverflow.com/questions/6342334/open-dropdownin-a-datagrid-view-items-on-a-single-click) it will solve your problem – andy Oct 22 '12 at 05:43

7 Answers7

68

I realize this is an old question, but I figured I would give my solution to anyone out there that may need to be able to do this.

While I couldn't find any answers to do exactly this... I did find an answer to a different question that helped me.

This is my solution:

private void datagridview_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        bool validClick = (e.RowIndex != -1 && e.ColumnIndex != -1); //Make sure the clicked row/column is valid.
        var datagridview = sender as DataGridView;

        // Check to make sure the cell clicked is the cell containing the combobox 
        if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validClick)
        {
            datagridview.BeginEdit(true);
            ((ComboBox)datagridview.EditingControl).DroppedDown = true;
        }
    }


private void datagridview_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        datagridview.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }

The above code must be tied into the CellEnter event of the datagridview.

I hope this helps!

edit: Added a column index check to prevent crashing when the entire row is selected.

Thanks, Up All Night for the above edit

edit2: Code is now to be tied to the CellEnter rather than the CellClick event.

Thanks, HaraldDutch for the above edit

edit3: Any changes will committed immediately, this will save you from clicking in another cell in order to update the current combobox cell.

H Aßdøµ
  • 2,925
  • 4
  • 26
  • 37
Jeff Click
  • 889
  • 9
  • 20
  • 6
    This helps a lot and in my opinion is just the right way to obtain it. `gridView.EditMode = DataGridViewEditMode.EditOnEnter` opens the dropdown only clicking on the arrow. – ceztko Jan 27 '15 at 10:59
  • 1
    I ran into this issue on numerous occasions and it turns out THIS is the correct (and most likely the only sensible answer for most users) answer. For others running into this issue, please do a direct comparison between this answer and the most up-voted answer here. You will definitely see a large difference. – TtT23 Nov 10 '15 at 04:27
  • 3
    Alas this does not work if you use the keyboard to navigate through the cells. Use event CellEnter instead of cell cick – Harald Coppoolse Apr 05 '16 at 09:43
  • @HaraldDutch Thank you! I will adjust my answer. – Jeff Click Apr 07 '16 at 12:03
  • Yo, you my man! I've been seriously thinking Windows Forms controls are total pain, was thinking why I need to click x times to get my dropdown showing. – Radityo Ardi Jan 03 '23 at 03:43
30

Set the following on your DataGridView:

EditMode = EditOnEnter

This is probably the easiest solution and has been the workaround for many users here on SO when this question gets asked.


EDIT :

Per here do the following:

Set the Editmode:

EditMode = EditOnKeystrokeOrF2

Modify the EditingControlShowing event on the datagridview:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox ctl = e.Control as ComboBox;
    ctl.Enter -= new EventHandler(ctl_Enter);
    ctl.Enter += new EventHandler(ctl_Enter);

}

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

This will get you your desired results. Let me know if that doesn't do it.

Community
  • 1
  • 1
KreepN
  • 8,528
  • 1
  • 40
  • 58
  • This is what I did. Still requires the user to focus the control first before activating the dropdown list. – TtT23 Oct 22 '12 at 06:18
  • It should activate on the first click with that change. Does it do that for you? It seems to focus and bring up the drowpdown for me when the settings are adjusted to the above. – KreepN Oct 22 '12 at 06:18
  • 2
    It does that if you click on the arrow. I want this behavior to occur when the entire cell has been clicked (I.E: Text Part) – TtT23 Oct 22 '12 at 06:32
  • @l46kok I think you may have something additional getting in your way then. Go ahead and make a fresh winforms app, add 1 unbound comboboxxolumn and try. You'll see that the second one works fine. – KreepN Oct 23 '12 at 00:40
5

I changed only the EditMode property of the datagridview to EditOnEnter and it's working perfectly.

EditMode  = EditOnEnter
beaver
  • 17,333
  • 2
  • 40
  • 66
Yudit
  • 51
  • 1
  • 2
1

If you set the entire grid to EditOnEnter, you can get some pretty funky activity when you are on a text column. Here's my solution, which should be self explanatory. If you did not know the column names, you could just check the cell type on mousemove.

Private Sub GridView_CellMouseMove(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles GridView.CellMouseMove
    Select Case GridView.Columns(e.ColumnIndex).Name
        Case "Ad_Edit", "Size_Caption", "Demo_Code"
            GridView.EditMode = DataGridViewEditMode.EditOnEnter
        Case Else
            GridView.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2
    End Select
End Sub
jefferp
  • 21
  • 2
0

Set the DropDownStyle property of your combo box to DropDownList...

Abdul Majid
  • 95
  • 1
  • 10
  • This is what I did. Still requires the user to click twice if the combobox does not have focus to begin with – TtT23 Oct 22 '12 at 05:23
  • @l46kok, any events are associated with combobox? – andy Oct 22 '12 at 05:33
  • @Anandkumar Nope. Just plain old combobox with the above mentioned property changed. Edit: omg I forgot to mention that this is for DGV's combobox. Sorry. – TtT23 Oct 22 '12 at 05:35
  • @Anandkumar I'm assuming you meant my IDE? It's VS2008 Professional – TtT23 Oct 22 '12 at 05:41
0

Perhaps old.. But make sure to set ReadOnly property to false, else the cell wont enter editmode and therefore the EditingControl returns null and casting DroppedDown = true will cast a NullReferencException.

Luuk
  • 1
0

I am using this solution in my datagridviews.

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If DataGridView1(e.ColumnIndex, e.RowIndex).EditType = GetType(DataGridViewComboBoxEditingControl) Then
            DataGridView1.BeginEdit(True)
            Dim comboboxCell As DataGridViewComboBoxEditingControl = DataGridView1.EditingControl
            If comboboxCell Is Nothing Then Return
            comboboxCell.DroppedDown = True
        End If
    End Sub
gegy
  • 87
  • 7