在Combobox上更改事件VB.NET上显示DataGridViewComboBoxCell的ValueMember

时间:2012-03-19 15:22:30

标签: vb.net datagridview combobox valuemember

我正在使用此代码:

Dim cListItems As New System.Collections.Generic.List(Of Combobox_values)

        cListItems.Add(New Combobox_values("One", "1"))
        cListItems.Add(New Combobox_values("Two", "2"))

        Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(DataGridView1.Rows(0).Cells(0), DataGridViewComboBoxCell)
        dgvcbc.DataSource = cListItems
        dgvcbc.DisplayMember = "Text"
        dgvcbc.ValueMember = "Value"

此代码工作正常,但我想要一个事件,以便在更改时显示所选项目的值。

非常感谢

1 个答案:

答案 0 :(得分:1)

检查一下:

首先声明此Vars:

Dim comboBoxCol As New DataGridViewComboBoxColumn
    Dim gol As Integer = 0

Private Sub discountitems_new_discount_EditingControlShowing1(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles discountitems_new_discount.EditingControlShowing
        current_row = discountitems_new_discount.CurrentRow.Index.ToString
        comboBox = CType(e.Control, ComboBox)

        If (comboBox IsNot Nothing) Then

            'Add the event handler.  
            'AddHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
            gol = 1
            AddHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
        End If
    End Sub

  Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        comboBox = CType(sender, ComboBox)
        If gol = 1 Then
            Dim item_value As String = comboBox.SelectedValue 'Value
msgbox(item_value)
            gol = 0
        End If
    End Sub