从数据库查询填充 ComboBox [英] Populate ComboBox from Database query
问题描述
我看到很多类似问题的答案,人们说为了获得组合框中加载的项目的价值,您需要使用
I see many of the answers to similar question where people are saying that in order to get the value of the item loaded in combobox you need to use
combobox1.displayMamer =""
combobox1.valuemember=""
combobox1.datasource=""
但是这个东西不起作用.....
But this stuff does not work.....
这是我所拥有的......
here is what I have....
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
我从表中选择了代码1和code2,我希望能够显示代码1,选择时,我希望能够拥有code2的值,但是用displaymember和valuemember,我没有看到任何结果.
I'm selecting Code1 and Code2 from Table, i want to be able to display code1 and when selected, I want to be able to have the value of Code2, but with displayMember, and ValueMember, I'm not seeing any result.
这是我所有的代码:
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) --- 这一行带有下划线
Dim rows = dt.Load(com.ExecuteReader) --- this line gets underlined
ERROR 说:表达式不产生值
ERROR says: Expression does not produce value
编辑 2:
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"
Now I get another error saying that: Cannot bind to the new value member. This happens on combobox1.valuemember="Label"
推荐答案
您可以将 DataTable
绑定到控件以用作 Datasource代码>.然后,您可以告诉它在有选择时显示哪个元素以及提交给您的值:
Rather than populating the items collection, you can bind a DataTable
to the control to be used as the Datasource
. Then, you can tell it which element to display and which value to submit to you when there is a selection:
Using con As New SqlConnection(sConnection)
Using com As New SqlCommand("Select Id, Name FROM ....", con)
con.Open()
Dim dt As New DataTable()
dt.Load(com.ExecuteReader)
cbox1.Datasource = dt
cbox.DisplayMember = "Name"
cbox.ValueMember = "Id"
End Using
End Using
Name"和Id"将是数据库表中的列名.在这种情况下,您可能希望使用的事件是 SelectedValueChanged
,而 SelectedValue
将保存与所选项目相关的 ID.这将作为 Object
返回,因此您可能需要将其转换回任何内容.
"Name" and "Id" would be column names from the database table. The event you probably want to work with in this case would be the SelectedValueChanged
and SelectedValue
would hold the ID related to the item selected. This will be returned as Object
so you may need to cast it back to whatever it is.
您也可以以同样的方式绑定到 List(Of T)
集合.在这种情况下,SelectedItem
可以是整个对象.例如,使用 List(of Employee)
,SelectedItem
将是用户选择的 Employee 的对象.
You can also bind to List(Of T)
collections in the same way. In that case, the SelectedItem
could be the entire object. For instance, using a List(of Employee)
, SelectedItem
would be the object for the Employee the user selected.
这篇关于从数据库查询填充 ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!