How to activate combobox on first click (Datagridview)

后端 未结 6 648
故里飘歌
故里飘歌 2020-12-13 12:48

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 c

相关标签:
6条回答
  • 2020-12-13 13:22

    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.

    0 讨论(0)
  • 2020-12-13 13:26

    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.

    0 讨论(0)
  • 2020-12-13 13:30

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

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-13 13:40

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

    EditMode  = EditOnEnter
    
    0 讨论(0)
  • 2020-12-13 13:40

    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.

    0 讨论(0)
提交回复
热议问题