How to Detect ComboBox Selected Value in DataGridView
Detecting the selected value of ComboBox is done by using the event like
SelectionChangeCommitted. This event is being fired once you select a value in a ComboBox.
However, if you have a ComboBox column in DataGridView control, you cannot easily use those events without adding a Handler. So, in this case, you need to create a procedure that will listen as an event to a handler that you have created.
Here’s an example code on how to detect the selected value of a ComboBox column in DataGridView control.
- Private Sub ProductDetailDataGridView_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles ProductDetailDataGridView.EditingControlShowing
- Dim combo As ComboBox = TryCast(e.Control, ComboBox)
- If combo IsNot Nothing Then
- RemoveHandler combo.SelectionChangeCommitted, AddressOf ComboBox_SelectionChangeCommitted
- AddHandler combo.SelectionChangeCommitted, AddressOf ComboBox_SelectionChangeCommitted
- End If
- End Sub
- Private Sub ComboBox_SelectionChangeCommitted(sender As Object, e As EventArgs)
- Dim cb As ComboBox = DirectCast(sender, ComboBox)
- Dim strValue As String = cb.Text
- If strValue IsNot Nothing Then
- End If
- End Sub
In the example above, we created a procedure called
ComboBox_SelectionChangeCommitted. This event is being fired once you select a value in ComboBox inside a DataGridView control. This procedure listens to an event after you declare a new handler
AddHandler combo.SelectionChangeCommitted, AddressOf ComboBox_SelectionChangeCommitted
I used the
SelectionChangeCommitted to prevent the event from firing when you select another row in DataGridView control.