链接两个组合框vb网

时间:2017-01-24 12:30:04

标签: vb.net combobox

当combobox1更改值

时,需要更改Combobox2值
    Private Sub ComboBoxPodelba_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxPodelba.SelectedIndexChanged

            Dim myconnect As New SqlConnection
            myconnect.ConnectionString = Common.connectionString

            Dim mycommand2 As SqlCommand = New SqlCommand()
            mycommand2.Connection = myconnect

            Try
                myconnect.Open()
                mycommand2.CommandText = "select PodPodelbaIme from PodPodelba where PodelbaFK = '" + ComboBoxPodelba.SelectedValue.ToString + "'"

                Dim reader2 As SqlDataReader
                reader2 = mycommand2.ExecuteReader()
                Dim dt2 As DataTable = New DataTable
                dt2.Load(reader2)
                ComboBoxPOdPodelba.ValueMember = "PodPodelbaIme"
                ComboBoxPOdPodelba.DisplayMember = "PodPodelbaIme"
                ComboBoxPOdPodelba.DataSource = dt2

            Catch ex As Exception

            End Try

        End Sub

1 个答案:

答案 0 :(得分:0)

...试

Private Sub Combo1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Combo1.SelectedIndexChanged

        Dim myconnect As New SqlConnection
        myconnect.ConnectionString = Common.connectionString

        Dim mycommand2 As SqlCommand = New SqlCommand()
        mycommand2.Connection = myconnect

        Try
            myconnect.Open()
            mycommand2.CommandText = "select PodPodelbaIme from PodPodelba where PodelbaFK = '" + Sender.SelectedValue.ToString + "'"

            Dim reader2 As SqlDataReader
            reader2 = mycommand2.ExecuteReader()
            Dim dt2 As DataTable = New DataTable
            dt2.Load(reader2)
            Combo2.ValueMember = "PodPodelbaIme"
            Combo2.DisplayMember = "PodPodelbaIme"
            Combo2.DataSource = dt2

        Catch ex As Exception

        End Try

    End Sub