0

在运行时创建一个 datagridview(Mydgv1)。其中的第一列是可编辑的组合框列。我已经为其设置了显示成员和值成员,但我无法获取正确的显示/值成员。当我从下拉列表中选择一个项目时,我将值/显示成员的值作为下拉列表中最后一个项目的值/显示成员。为什么值/显示成员不随组合中的选定项目而变化框列。

On Form Load Event

combo.HeaderText = "Item"
combo.Name = "itemid"
combo.Items.Clear()
Dim ds As SqlDataReader
Dim cmm As New SqlCommand("select itemid from itemdesc", con)
con.Open()
    ds = cmm.ExecuteReader
    If ds.HasRows Then
        While ds.Read
            combo.Items.Add(ds(0).ToString)
            combo.ValueMember = ds("itemid")
            combo.DisplayMember = ds("itemid")
        End While
    End If
con.Close()
Mydgv1.Columns.Add(combo)

On comboboxcolumn's Leave Event

it = combo.DisplayMember
MsgBox(it)
4

1 回答 1

0

不要引用显示成员本身并使用 SelectedIndexChanged 事件来检索选定的值

Private Sub combo_SelectedIndexChanged(sender as Object, e as EventArgs) Handels combo.SelectedIndexChanged

    'because your display and valuemember are set as the same
    MsgBox(combo.SelectedValue.ToString)
    'if you want the displaymember as the result use
    MsgBox(combo.Text)

End Sub
于 2014-09-09T09:31:04.087 回答