vb.netdatagridviewdatagridviewcomboboxdatagridviewcomboboxcolumn

Trigger selected value changed event on DataGridView.ComboBoxColumn


Currently, when a value is selected within the DataGridViewColumn with ComboBoxes, user has to click off that cell in order for the values to refresh via CellValueChanged Event on the DataGridView.

What I am trying to achieve is that as soon as a value is selected in a ComboBox the refresh is triggered.

Below is what I tried doing, so that when the drop-down is opened/closed it would trigger the refresh, but it only executes when the ComboBox is clicked on and the drop-down is visible, not when a value is selected.

Private Sub PL_DGV_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles PL_DGV.EditingControlShowing
        Dim cb As ComboBox = TryCast(e.Control, ComboBox)
        If cb IsNot Nothing Then
            Dim editingComboBox As ComboBox = DirectCast(e.Control, ComboBox)

            RemoveHandler editingComboBox.SelectedIndexChanged,
                New EventHandler(AddressOf editingComboBox_SelectedIndexChanged)

            AddHandler editingComboBox.SelectedIndexChanged,
                New EventHandler(AddressOf editingComboBox_SelectedIndexChanged)
        End If

End Sub

Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim dgvc As DataGridViewCell = TryCast(Me.PL_DGV.CurrentCell, DataGridViewCell)

    RefreshCarriage(dgvc.RowIndex)

End Sub

Solution

  • Based on comments from @jmcilhinney I have managed to find a thread which covers this in C#: Thread here

    My code following a read of the above thread:

    Private Sub PL_DGV_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles PL_DGV.CurrentCellDirtyStateChanged
                'My DGV Combo Box column is second within the grid therefore manually checking for index 1 column.      
                If PL_DGV.CurrentCell.ColumnIndex = 1 Then
                    If PL_DGV.IsCurrentCellDirty Then
                        PL_DGV.CommitEdit(DataGridViewDataErrorContexts.Commit)
                    End If
                End If
        End Sub
    

    So the above triggers the CellValueChanged event in which I have added another check for the DGV ComboBox column as I only need to run a the specific function when that column is is edited:

    Private Sub PL_DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles PL_DGV.CellValueChanged
                If PL_DGV.CurrentCell.ColumnIndex = 1 Then
                        RefreshCarriage(e.RowIndex)
                End If
    End Sub
    

    Again, thanks to @jmcilhinney for commenting and leading me to the solution of the issue.