I have a form that has a datagridview in it. I have bound this data with properties from a different class. The populating of the grid is working along with the populating from the dropdown is also working. The only question I have is it doesn’t matter what I do I always have to click twice to get the dropdown to dropdown. Is there anything I can do differently to get the cell to dropdown on the first click? Any help would be appreciated.
Private Property MountingPadDGVinfo As New BindingList(Of MountingPadInfoForDataGridView)
Private Sub BulkMountingPadEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Populate Type ComboBox column with Enum values
Dim padTypeColumn As DataGridViewComboBoxColumn = CType(DataGridView_MountingPads.Columns("Type"), DataGridViewComboBoxColumn)
padTypeColumn.Items.Clear()
padTypeColumn.Items.AddRange([Enum].GetNames(GetType(MountingPadTypeEnum)))
' Populate Side ComboBox column with Enum values
Dim padSideColumn As DataGridViewComboBoxColumn = CType(DataGridView_MountingPads.Columns("Side"), DataGridViewComboBoxColumn)
padSideColumn.Items.Clear()
padSideColumn.Items.AddRange([Enum].GetNames(GetType(MountingPadSideEnum)))
' Populate Pad1Angle ComboBox column with Double values
Dim pad1AngleColumn As DataGridViewComboBoxColumn = CType(DataGridView_MountingPads.Columns("Pad1Angle"), DataGridViewComboBoxColumn)
pad1AngleColumn.Items.Clear()
pad1AngleColumn.Items.AddRange(g_DefaultMountingPadAngles.Cast(Of Object).ToArray()) ' Ensuring it's casted to Object to prevent mismatch
' Populate Pad2Angle ComboBox column with Double values
Dim pad2AngleColumn As DataGridViewComboBoxColumn = CType(DataGridView_MountingPads.Columns("Pad2Angle"), DataGridViewComboBoxColumn)
pad2AngleColumn.Items.Clear()
pad2AngleColumn.Items.AddRange(g_DefaultMountingPadAngles.Cast(Of Object).ToArray())
' Populate PipeDia ComboBox column with Double values
Dim pipeDiaColumn As DataGridViewComboBoxColumn = CType(DataGridView_MountingPads.Columns("PipeDia"), DataGridViewComboBoxColumn)
pipeDiaColumn.Items.Clear()
pipeDiaColumn.Items.AddRange(g_PipeDia.Cast(Of Object).ToArray())
' Set the DataGridView edit mode to enter edit mode when a cell is selected
DataGridView_MountingPads.EditMode = DataGridViewEditMode.EditOnEnter
' Bind the DataGridView to the data source (BindingList)
DataGridView_MountingPads.DataSource = MountingPadDGVinfo
End Sub
Private Sub HandleDataGridViewMouseDown(sender As Object, e As MouseEventArgs)
' Determine where the click is occurring
Dim info As DataGridView.HitTestInfo = Me.DataGridView_MountingPads.HitTest(e.X, e.Y)
' Check if the click occurred in a cell
If info.Type = DataGridViewHitTestType.Cell Then
' Set the clicked cell as the current cell
Me.DataGridView_MountingPads.CurrentCell = Me.DataGridView_MountingPads.Rows(info.RowIndex).Cells(info.ColumnIndex)
End If
End Sub
Private Sub DataGridView_MountingPads_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView_MountingPads.CellEnter
' Check if the entered cell belongs to a ComboBox column
If e.ColumnIndex >= 0 AndAlso TypeOf DataGridView_MountingPads.Columns(e.ColumnIndex) Is DataGridViewComboBoxColumn Then
' Begin editing immediately on cell enter
DataGridView_MountingPads.BeginEdit(True)
' Get the editing control (should be a ComboBox)
Dim comboBox = TryCast(DataGridView_MountingPads.EditingControl, ComboBox)
If comboBox IsNot Nothing Then
' Make sure ComboBox is focused and force dropdown
comboBox.DroppedDown = False ' Reset the dropdown state
comboBox.Focus() ' Ensure ComboBox has focus
comboBox.DroppedDown = True ' Force dropdown to open
End If
End If
End Sub
Private Sub DataGridView_MountingPads_CellLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView_MountingPads.CellLeave
' End editing when leaving the cell
If e.ColumnIndex >= 0 AndAlso TypeOf DataGridView_MountingPads.Columns(e.ColumnIndex) Is DataGridViewComboBoxColumn Then
DataGridView_MountingPads.EndEdit()
End If
End Sub
Private Sub DataGridView_MountingPads_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView_MountingPads.EditingControlShowing
' Check if the control being shown is a ComboBox
If TypeOf e.Control Is ComboBox Then
Dim comboBox As ComboBox = DirectCast(e.Control, ComboBox)
comboBox.DropDownStyle = ComboBoxStyle.DropDownList ' Ensure it acts like a dropdown
End If
End Sub
Private Sub DataGridView_MountingPads_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DataGridView_MountingPads.DataError
' Suppress data errors
e.ThrowException = False
End Sub
I have tried the above code and it is still requiring two clicks.
Tiffany Hayden is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.