Combobox通过代码进行数据绑定

时间:2014-10-14 19:30:27

标签: vb.net combobox

我有这个表单,我将数据绑定到控件。这是我的代码

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Try
        conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=c:\users\desraj\desktop\mydb.accdb;")
        conn.Open()
        formload()

        adap = New OleDbDataAdapter("select * from mytable", conn)
        Dim ds As New DataSet
        adap.Fill(ds, "mytable")
        BindingSource1.DataSource = ds.Tables(0)
        BindingNavigator1.BindingSource = BindingSource1


        TextBox1.DataBindings.Add("Text", BindingSource1, "id")
        TextBox2.DataBindings.Add("Text", BindingSource1, "myname")


        ComboBox1.DataBindings.Add("Text", BindingSource1, "city_code")
        ComboBox2.DataBindings.Add("Text", BindingSource1, "state_code")


    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try

End Sub

Public Sub formload()
    Try
        cmd = New OleDbCommand("select * from mycity", conn)
        reader = cmd.ExecuteReader
        While reader.Read = True
            ComboBox1.Items.Add(reader.Item(1))
        End While
        reader.Close()

        cmd = New OleDbCommand("select * from mystate", conn)
        reader = cmd.ExecuteReader
        While reader.Read = True
            ComboBox2.Items.Add(reader.Item(1))
        End While
        reader.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub


我有3张桌子 表1:MYTABLE有4列
id,myname,city_code(fk),state_code(fk)
表2:MYCITY有2列
city_code,city
表3:MYSTATE有2列
state_code,state

例如:
mytable具有以下值 ID myname city_code state_code
1 alan 2 3
2摇滚3 1 3 brock 1 2
mycity具有以下值 city_code city
1 abc
2 xyz
3 pqr
mystate具有以下值 state_code state
1 lmn
2 fgh
3 def

问题在于,它不会加载combobox1combobox2城市和州,而是加载city_codestate_code。 如何在citystate中加载combobox1combobox2,因为我的外键位于mytable

2 个答案:

答案 0 :(得分:2)

首先应通过设置DisplayMember,ValueMember和DataSource将父列表绑定到ComboBox。 ValueMember是PK的名称。然后使用DataBindings将SelectedValue绑定到子表的FK列,例如。

With cityComboBox
    .DisplyMember = "city"
    .ValueMember = "city_code"
    .DataSource = cityBindingSource
    .DataBindings.Add("SelectedValue", personBindingSource, "city_code")
End With

请注意,使用了两个不同的BindingSource。

答案 1 :(得分:1)

我希望这就是你想要的。我创建了一个你可以在你的班级或任何形式中玷污的子。

Public Sub ComboData(ByVal FieldName As String, ByVal TableName As String, ByVal ComboBoxName As Object)
Try
        Dim conSQL As New SqlConnection
        conSQL.ConnectionString = conString1 'this is the connectionString you defined, change the name to yours.
        conSQL.Open()

        cmdSQL.Connection = conSQL
        cmdSQL.CommandText = "Select " & FieldName & " from " & TableName

        drSQL = cmdSQL.ExecuteReader()

        ' Fill a combo box with the datareader
        Do While drSQL.Read = True
            ComboBoxName.Items.Add(drSQL.GetString(0))
        Loop
        'Con.Dispose()

        conSQL.Close()
        'End If
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try
End Sub

在此之后使用。

c.ComboData("<add the field name>", "<add the table name>", <add the combobox you want to bring the desired information>)

我希望这是答案,如果不是更新我,我会相应地编辑我的答案。