在运行时创建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)
答案 0 :(得分:0)
不要引用displaymember本身并使用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