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.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.