从数据库查询填充组合框 [英] Populate ComboBox from Database query

查看:62
本文介绍了从数据库查询填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了许多类似问题的答案,人们都说要获得组合框中加载的物品的价值,您需要使用

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

我正在从表中选择Code1和Code2,我希望能够显示code1,并且当我被选中时,我希望能够具有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.

编辑:这是我的所有代码:

Here's all my code:

    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行= dt.Load(com.ExecuteReader )---这行带有下划线

Dim rows = dt.Load(com.ExecuteReader) --- this line gets underlined

错误提示:表达式不产生值

ERROR says: Expression does not produce value

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

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

名称和 Id将是数据库表。在这种情况下,您可能要使用的事件将是 SelectedValueChanged SelectedValue 将保存与所选项目。这将作为 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 可能是整个对象。例如,使用(员工列表) SelectedItem 将成为用户所选员工的对象。

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.

这篇关于从数据库查询填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆