如何处理DataGridViewComboBox SelectedIndex更改事件?

时间:2015-07-20 12:56:27

标签: vb.net winforms

我有一个DataGridView它有两个DataGridViewComboBoxColumn我想处理两个不同的selectedindexchanged事件

我怎样才能实现?

在Winform VB.net中

1 个答案:

答案 0 :(得分:2)

可以区分列索引点击哪个组合框列。小例子:

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