0

I have a One DataGridView it has Two DataGridViewComboBoxColumn i want to handle selectedindexchanged event of both differently

how can i achieve ?

IN Winform VB.net

Bhavin
  • 240
  • 7
  • 19
  • I believe you can find your answers in other questions already asked, for example see [here](https://stackoverflow.com/questions/11141872/event-that-fires-during-datagridviewcomboboxcolumn-selectedindexchanged). – Saragis Jul 20 '15 at 13:42
  • i already go through this its work fine with one combobox column but i have to combobox in datagridview and that not manged with this – Bhavin Jul 21 '15 at 04:08
  • I have TWO combobox in datagridview .... – Bhavin Jul 21 '15 at 07:39
  • See the example I posted below. – Saragis Jul 21 '15 at 07:57

1 Answers1

2

It's possible to differentiate between which combobox-column was clicked by the columnindex. Small example:

Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dataGridView1.EditingControlShowing
    If TypeOf e.Control Is ComboBox Then
        If dataGridView1.CurrentCell.ColumnIndex = 1 Then
            Dim cb As ComboBox = TryCast(e.Control, ComboBox)

            'remove handler if it was added before
            RemoveHandler cb.SelectedIndexChanged, AddressOf ColumnCombo1SelectionChanged
            AddHandler cb.SelectedIndexChanged, AddressOf ColumnCombo1SelectionChanged
        ElseIf dataGridView1.CurrentCell.ColumnIndex = 2 Then
            Dim cb As ComboBox = TryCast(e.Control, ComboBox)

            'remove handler if it was added before
            RemoveHandler cb.SelectedIndexChanged, AddressOf ColumnCombo2SelectionChanged
            AddHandler cb.SelectedIndexChanged, AddressOf ColumnCombo2SelectionChanged
        End If
    End If
End Sub

Private Sub ColumnCombo1SelectionChanged(sender As Object, e As EventArgs)
    Dim sendingComboEdit = TryCast(sender, DataGridViewComboBoxEditingControl)
    Dim selectedValue As Object = sendingComboEdit.SelectedValue
End Sub

Private Sub ColumnCombo2SelectionChanged(sender As Object, e As EventArgs)
    Dim sendingComboEdit = TryCast(sender, DataGridViewComboBoxEditingControl)
    Dim selectedValue As Object = sendingComboEdit.SelectedValue
End Sub
Saragis
  • 1,782
  • 6
  • 21
  • 30