Skip to main content

How to Detect ComboBox Selected Value in DataGridView

Body
Detecting the selected value of ComboBox is done by using the event like SelectedIndexChanged, SelectedValueChanged, and 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.
  1. Private Sub ProductDetailDataGridView_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles ProductDetailDataGridView.EditingControlShowing
  2. Dim combo As ComboBox = TryCast(e.Control, ComboBox)
  3.  
  4. If combo IsNot Nothing Then
  5. RemoveHandler combo.SelectionChangeCommitted, AddressOf ComboBox_SelectionChangeCommitted
  6. AddHandler combo.SelectionChangeCommitted, AddressOf ComboBox_SelectionChangeCommitted
  7. End If
  8. End Sub
  9.  
  10. Private Sub ComboBox_SelectionChangeCommitted(sender As Object, e As EventArgs)
  11. Dim cb As ComboBox = DirectCast(sender, ComboBox)
  12. Dim strValue As String = cb.Text
  13.  
  14. If strValue IsNot Nothing Then
  15. MessageBox.Show(strValue)
  16. End If
  17. 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.

Comments

Submitted byWapakels Gwapo (not verified)on Thu, 08/15/2013 - 15:09

Can you send me the code for log in for different users such as client, admin, and client. this is my email : [email protected]

Add new comment