级联组合框

时间:2016-06-24 15:56:26

标签: sql vb.net combobox

我正在尝试创建一个从SQL数据集填充的级联ComboBox。 我的问题是编译代码时没有出现任何错误。但是这两个ComboBox并没有按照预期的数据填充。你能帮我找到我犯错误的地方吗?

Private Sub FillSalesPerson()
    Dim strConnString As String
    strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
    Dim DS As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand()
    cmd.Connection = DS
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "select distinct Name,Salesperson_Code  from dbo.View_Customers_With_Sales_People"
    Dim objeDS As DataSet = New DataSet()
    Dim dAdapter As New SqlDataAdapter
    dAdapter.SelectCommand = cmd
    DS.Open()
    dAdapter.Fill(objeDS)
    DS.Close()

    ComboBox1.ValueMember = "Salesperson_Code"
    ComboBox1.DisplayMember = "Name"
    ComboBox1.DataSource = objeDS.Tables(0)
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    If ComboBox1.SelectedValue.ToString() <> " " Then
        Dim Salesperson_Code As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
        FillStates(Salesperson_Code)
        'ComboBox2.SelectedIndex = 0'
    End If
End Sub

Private Sub FillStates(Salesperson_Code As Integer)
    Dim strConnString As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
    Dim DS As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand()
    cmd.Connection = DS
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "Select [No_],[Company Name] from [dbo].[View_Customers_With_Sales_People] where (Salesperson_Code= @Salesperson_Code)"
    cmd.Parameters.AddWithValue("@Salesperson_Code", Salesperson_Code)
    Dim objDs As New DataSet()
    Dim adapter As New SqlDataAdapter()
    adapter.SelectCommand = cmd
    DS.Open()
    adapter.Fill(objDs)
    DS.Close()
    If objDs.Tables(0).Rows.Count > 0 Then
        ComboBox2.ValueMember = "[No_]"
        ComboBox2.DisplayMember = "[Company Name]"
        ComboBox2.DataSource = objDs.Tables(0)
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

尝试更改ComboBox1.DataSource = objeDS.Tables(0)

ComboBox1.RowSource = objeDS.Tables(0)