Vb.net 通过代码进行组合框数据绑定

Vb.net 通过代码进行组合框数据绑定,vb.net,combobox,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;")

我有一个表单,其中我将数据绑定到控件。这是我的密码

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

我有三张桌子
表1:
MYTABLE
共4列
id、myname、城市代码(fk)、州代码(fk)

表2:
MYCITY
有两列
city\u代码,city

表3:
MYSTATE
有两列
状态\u代码,状态


例如:
mytable
具有以下值
ID
myname
city\u code
state\u code

1艾伦2 3
2岩石3 1
3布洛克12
mycity
具有以下值
city\u code
city

1
abc

2
xyz

3
pqr

mystate
具有以下值
状态\u代码
状态

1
lmn

2
fgh

3
def

问题是,它没有加载城市和州的
combobox1
Combox2
,而是加载
city\u code
state\u code

如何在
combobox1
combobox2
中加载
city
state
,因为我在
mytable
中有它们的外键?

您应该首先通过设置DisplayMember、ValueMember和DataSource将父列表绑定到组合框。ValueMember是PK的名称。然后使用数据绑定将SelectedValue绑定到子表的FK列,例如

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

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

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

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

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

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

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

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

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

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

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

我希望这就是您想要的。我创造了一个潜艇,你可以在你的班级或任何形式的玷污

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>)
c.ComboData(“,”,)

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

我希望这是您想要的答案。我创造了一个潜艇,你可以在你的班级或任何形式的玷污

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>)
c.ComboData(“,”,)

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

我希望这是您想要的答案。我创造了一个潜艇,你可以在你的班级或任何形式的玷污

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>)
c.ComboData(“,”,)

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

我希望这是您想要的答案。我创造了一个潜艇,你可以在你的班级或任何形式的玷污

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>)
c.ComboData(“,”,)

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

你是说你希望CBO1有类似于{阿拉斯加,AK;阿拉巴马,AL…}的东西,这样你就可以将城市和州分配给其他东西?你是说你希望CBO1有类似于{阿拉斯加,AK;阿拉巴马,AL…}的东西吗所以你可以把城市和州分配给其他东西?你是说你想让CBO1有像{阿拉斯加,AK;阿拉巴马,AL…}这样你就可以把城市和州分配给其他东西?你是说你想让CBO1有像{阿拉斯加,AK;阿拉巴马,AL…}这样你就可以把城市和州分配给其他东西?谢谢@jmchilinney。实际上,你能告诉我
DisplayMember
ValueMember
之间有什么区别吗?为什么你不能同时阅读它们的文档呢?先看,然后再问问题。。。只有当你真的需要的时候。好的…谢谢你…但我还有一个疑问。请帮助我,如果我有4个这样的组合框,其中2个组合显示城市和2个显示州。我应该创建4个不同的bindingsource吗?谢谢@jmcilhinney。实际上,你能告诉我
DisplayMember
ValueMember
之间有什么区别吗?为什么你不能同时阅读它们的文档呢?先看,然后再问问题。。。只有当你真的需要的时候。好的…谢谢你…但我还有一个疑问。请帮助我,如果我有4个这样的组合框,其中2个组合显示城市和2个显示州。我应该创建4个不同的bindingsource吗?谢谢@jmcilhinney。实际上,你能告诉我
DisplayMember
ValueMember
之间有什么区别吗?为什么你不能同时阅读它们的文档呢?先看,然后再问问题。。。只有当你真的需要的时候。好的…谢谢你…但我还有一个疑问。请帮助我,如果我有4个这样的组合框,其中2个组合显示城市和2个显示州。我应该创建4个不同的bindingsource吗?谢谢@jmcilhinney。实际上,你能告诉我
DisplayMember
ValueMember
之间有什么区别吗?为什么你不能同时阅读它们的文档呢?先看,然后再问问题。。。只有当你真的需要的时候。好的…谢谢你…但我还有一个疑问。请帮助我,如果我有4个这样的组合框,其中2个组合显示城市和2个显示州。我应该创建4个不同的bindingsource吗。?