How to open ComboBox (DropDown it) hosted in DataGridView with single mouse click.

The usage of ComboBox in a DataGridView delivers some limitations of its usage. Very common problem is opening the ComboBox with single click of the mouse. By default double mouse click is needed because the first click selects the cell and the second click enters into Edit mode where the ComboBox fires its DropDown event.

We can do this using DataGridView.CellClick event. The following code is enough to open the ComboBox and fire its DropDown event.


Private Sub DataGridView1_CellClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Handles DataGridView1.CellClick
'enters into edit mode
DataGridView1.BeginEdit(True)

'checking if the editing control is a combobox
If TypeOf (DataGridView1.EditingControl) Is ComboBox Then

'casting the editing control and fire DropDown event
CType(DataGridView1.EditingControl, ComboBox).DroppedDown = True

End If
End Sub


DataGridView.EditingControl represents the control hosted by the current cell.

1 comment:

  1. Very nice. It works.

    Ahsan
    Islamabad, Pakistan

    ReplyDelete