.net 从数据库查询填充组合框
我看到许多类似问题的答案,人们说为了获得combobox中加载的项目的值,您需要使用.net 从数据库查询填充组合框,.net,vb.net,data-binding,combobox,.net,Vb.net,Data Binding,Combobox,我看到许多类似问题的答案,人们说为了获得combobox中加载的项目的值,您需要使用 combobox1.displayMamer ="" combobox1.valuemember="" combobox1.datasource="" 但是这些东西不起作用 这是我的 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Using con As New SqlCo
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和ValueMember,我看不到任何结果
编辑:以下是我的全部代码:
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)---此行带下划线
错误说明:表达式不产生值
编辑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”上,您可以将
数据表
绑定到要用作数据源的控件,而不是填充items集合。然后,当有选择时,您可以告诉它要显示的元素和要提交给您的值:
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。这将作为对象返回
,因此您可能需要将其转换回任何对象
您还可以以相同的方式绑定到List(Of T)
集合。在这种情况下,SelectedItem
可以是整个对象。例如,使用列表(员工)
,SelectedItem
将是用户选择的员工的对象 您可以将数据表
绑定到用作数据源的控件,而不是填充items集合。然后,当有选择时,您可以告诉它要显示的元素和要提交给您的值:
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。这将作为对象返回
,因此您可能需要将其转换回任何对象
您还可以以相同的方式绑定到List(Of T)
集合。在这种情况下,SelectedItem
可以是整个对象。例如,使用列表(员工)
,SelectedItem
将是用户选择的员工的对象 我不应该检查是否有任何数据吗?或循环通过?无需,除非应用程序需要在没有相关数据时执行某些操作。如果dt.Load
中的行为0,则没有匹配的行-但不需要填充循环。我不应该检查是否有任何数据吗?或循环通过?无需,除非应用程序需要在没有相关数据时执行某些操作。如果dt.Load
中的行数为0,则不存在匹配的行-但不需要填充循环。您不需要HasRows检查Do While rdr.Read
如果没有任何行,将跳过循环。将其更改为使用实际的SQL列、表名WHERE子句等。我将其更改为提供更通用的解决方案,因为我不知道data@plutonix-我确实更改了SQL语句,我在粘贴到这里之前删除了它,去掉了行的东西——我把它和填充数据表的不同方法搞混了。如果需要行数,请使用dt.rows.Count
。sorry@plutonix请参阅第二版。不用担心,谢谢你的帮助!你不需要那张支票Do While rdr.Read
如果没有任何行,将跳过循环。将其更改为使用实际的SQL列、表名WHERE子句等。我将其更改为提供更通用的解决方案,因为我不知道data@plutonix-我确实更改了SQL语句,我在粘贴到这里之前删除了它,去掉了行的东西——我把它和填充数据表的不同方法搞混了。如果需要行数,请使用dt.rows.Count
。sorry@plutonix请参阅第二版。不用担心,谢谢你的帮助!