• We just launched and are currently in beta. Join us as we build and grow the community.

How to Detect ComboBox Selected Value in DataGridView

boobies69

Site Builder
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
134
Likes
97
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. If

    combo IsNot

    Nothing

    Then
  4. RemoveHandler

    combo.

    SelectionChangeCommitted

    , AddressOf

    ComboBox_SelectionChangeCommitted
  5. AddHandler

    combo.

    SelectionChangeCommitted

    , AddressOf

    ComboBox_SelectionChangeCommitted
  6. End

    If
  7. End

    Sub

  8. Private

    Sub

    ComboBox_SelectionChangeCommitted(

    sender As

    Object

    , e As

    EventArgs)
  9. Dim

    cb As

    ComboBox =

    DirectCast

    (

    sender, ComboBox)
  10. Dim

    strValue As

    String

    =

    cb.

    Text

  11. If

    strValue IsNot

    Nothing

    Then
  12. MessageBox.

    Show

    (

    strValue)
  13. End

    If
  14. 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.

 

452,292

323,341

323,350

Top