用于导航两个表的vb.net bindingSource

时间:2014-01-07 06:15:33

标签: combobox navigation bindingsource

我正在使用VB.Net从SQL服务器,机构和国家/地区加载数据,并填充数据集中的每个表。国家/地区表具有countryId,countryName,其中countryId作为主键。 Institute具有instId,name,countryId列,其中countryId作为从国家/地区表中获取的外键。我正在使用bindingSource来浏览表机构中的数据并在文本框中显示数据。另一方面,我有一个组合框,它也有一个bindingSource,它是country table。我想现在,当我浏览机构表时,我也可以根据机构countryId中的值更改我的combobx中的数据。 请查看以下代码:

Private Sub frmSystemOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dsOptions = New DataSet
            loadOptions()

            bsInstitute = New BindingSource(dsOptions, "institute")

            InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId")
            NameTextBox.DataBindings.Add("Text", bsInstitute, "name")
            CountryIdTextBox.DataBindings.Add("Text", bsInstitute, "countryId")

            bsCountry = New BindingSource(dsOptions, "country")

            cmbCountry.DataSource = bsCountry
            cmbCountry.DisplayMember = "countryName"
            cmbCountry.ValueMember = "countryId"

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
    End Sub

    Sub loadOptions()
        Dim sql As String

        Try
            sqlConn = New SqlConnection(connString)
            sqlConn.Open()

            sql = "select instId, name, countryId from institute"
            daInstitute = New SqlDataAdapter(sql, sqlConn)
            daInstitute.Fill(dsOptions, "institute")
            '----------------------------------------------------------------------

            sql = "select countryId, countryName from country"
            daAdapter = New SqlDataAdapter(sql, sqlConn)
            daAdapter.Fill(dsOptions, "country")
            '----------------------------------------------------------------------

            sqlConn.Close()
        Catch ex As Exception
            sqlConn.Close()
            MsgBox(Err.Description)
        End Try
    End Sub

另外,对于导航,我使用以下代码:

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If bsInstitute.Position + 1 < bsInstitute.Count Then
            bsInstitute.MoveNext()
        Else
            bsInstitute.MoveFirst()
        End If
        Me.Invalidate()
    End Sub

现在,我如何根据institute表中countryId的值更改combobx中的countryName?

1 个答案:

答案 0 :(得分:0)

您需要按如下方式绑定组合框:

Private Sub frmSystemOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dsOptions = New DataSet
            loadOptions()

            bsInstitute = New BindingSource(dsOptions, "institute")
            bsCountry = New BindingSource(dsOptions, "country")

            InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId")
            NameTextBox.DataBindings.Add("Text", bsInstitute, "name")
            CountryIdTextBox.DataBindings.Add("Text", bsInstitute, "countryId")

            cmbCountry.DataSource = bsCountry
            cmbCountry.DisplayMember = "countryName"
            cmbCountry.ValueMember = "countryId"

            cmbCountry.DataBindings.Add(New Binding("SelectedValue", _
             Me.bsInstitute, "countryId", True))

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
    End Sub

    Sub loadOptions()
        Dim sql As String

        Try
            sqlConn = New SqlConnection(connString)
            sqlConn.Open()

            sql = "select instId, name, countryId from institute"
            daInstitute = New SqlDataAdapter(sql, sqlConn)
            daInstitute.Fill(dsOptions, "institute")
            '----------------------------------------------------------------------

            sql = "select countryId, countryName from country"
            daAdapter = New SqlDataAdapter(sql, sqlConn)
            daAdapter.Fill(dsOptions, "country")
            '----------------------------------------------------------------------

            sqlConn.Close()
        Catch ex As Exception
            sqlConn.Close()
            MsgBox(Err.Description)
        End Try
    End Sub