【发布时间】:2016-10-19 19:22:29
【问题描述】:
我看到很多类似问题的答案,人们说为了获得加载到组合框中的项目的价值,您需要使用
combobox1.displayMamer =""
combobox1.valuemember=""
combobox1.datasource=""
但是这玩意儿不行.....
这就是我所拥有的......
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using con As New SqlConnection(sConnection)
con.Open()
Using com As New SqlCommand("Select Code1, Code2 from tblTable6 where fldname ='Things'", con)
Using rdr = com.ExecuteReader
If rdr.HasRows Then
Do While rdr.Read = True
ComboBox1.Items.Add(rdr.GetString(0))
''''missing something here
Loop
con.Close()
End If
End Using
End Using
End Using
End Sub
我正在从表中选择Code1和code2,我希望能够显示Code1,并且在选择时,我希望能够具有Code2的值,但是使用DisplayMember,并且VealeMember,我看不到任何内容结果。
编辑:这是我所有的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using con As New SqlConnection(sConnection)
Using com As New SqlCommand("Select Label, Code from Table.....", con)
con.Open()
Dim dt As New DataTable()
Dim rows = dt.Load(com.ExecuteReader)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Code"
ComboBox1.ValueMember = "Label"
con.Close()
End Using
End Using
End Sub
Dim rows = dt.Load(com.ExecuteReader) --- 这行加下划线
ERROR 说:表达式不产生值
EDIT2:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using con As New SqlConnection(sConnection)
con.Open()
Using com As New SqlCommand("Select Label, Code from tblData where fldname ='M'", con)
Dim dt As New DataTable()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Code"
ComboBox1.ValueMember = "Label"
con.Close()
End Using
End Using
End Sub
现在我收到另一个错误消息:无法绑定到新值成员。 这发生在 combobox1.valuemember="Label"
【问题讨论】:
-
您不需要 HasRows 检查。如果没有任何行,
Do While rdr.Read将跳过循环。 -
更改它以使用您的实际 SQL - 列、表名 WHERE 子句等。我将其更改为提供更通用的解决方案,因为我不知道数据
-
@plutonix - 我确实更改了 SQL 语句,我在粘贴之前删除了它
-
摆脱行的东西 - 我把它与填充数据表的不同方式混淆了。如果您需要行数,请使用
dt.Rows.Count。对不起 -
@plutonix 请参阅edit2。不用担心,谢谢您的帮助!
标签: .net vb.net data-binding combobox