Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Visual Basic 2008中的动态下拉列表_Vb.net - Fatal编程技术网

Vb.net Visual Basic 2008中的动态下拉列表

Vb.net Visual Basic 2008中的动态下拉列表,vb.net,Vb.net,我有一个应用程序,它使用一组三个组合框。 根据上一个下拉列表的选定值,我已成功填充每个下拉列表 唯一的问题是,由于数据来自数据库,所以从第一个组合框中选择的两个值在数据库中可能有两个不同数量的项。因此,每次调用第一个组合框的selectedIndex\u changed nethod时,第二个组合框都应该填充这样的数据 例如,如果第一个组合框的第一项在第二个组合框中有10个对应项,而第一个组合框的第二项在第二个组合框中有三个对应项,在选择第一项之后选择第二项将导致出现第二个组合框,其中显示三项以

我有一个应用程序,它使用一组三个组合框。 根据上一个下拉列表的选定值,我已成功填充每个下拉列表 唯一的问题是,由于数据来自数据库,所以从第一个组合框中选择的两个值在数据库中可能有两个不同数量的项。因此,每次调用第一个组合框的selectedIndex\u changed nethod时,第二个组合框都应该填充这样的数据

例如,如果第一个组合框的第一项在第二个组合框中有10个对应项,而第一个组合框的第二项在第二个组合框中有三个对应项,在选择第一项之后选择第二项将导致出现第二个组合框,其中显示三项以及三项之后的七个空条目

我想让第二个组合框加载三项,而第一个组合框中的第二项在数据库中有三项

这里有一个例子

Private Sub cboAccountGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles cboAccountGroup.SelectedIndexChanged
    If mblnAccountGroupFirstLoad = True Then
        Exit Sub
    End If
    Dim index As String
    index = cboAccountGroup.SelectedValue
    'Call clearDataEntries()
    Dim psql As String
    psql = "Select Account_Type_ID,Description from Tbl_Account_Type Where Account_Group_id=" & index
    Call setDropDowns(cboAccountType, psql)
    mblnAccountTypeFirstLoad = False

End Sub
setDropDowns的定义如下

 Public Sub setDropDowns(ByRef combo As ComboBox, ByVal sql As String)


    Try
        Dim adaptor As New SqlDataAdapter(sql, mcon)
        Dim dataset As New DataTable()
        mcon.Open()
        adaptor.Fill(DataSet)
        mcon.Close()

        combo.DataSource = DataSet
        combo.DisplayMember = DataSet.Columns(1).ColumnName
        combo.ValueMember = DataSet.Columns(0).ColumnName


    Catch ex As Exception
    Finally
        'mcon.Close()

    End Try

End Sub

请协助。

我会稍微修改代码,使其看起来更像这样:

'Build functions that return data, and keep them separate from code that updates your UI
Public Function GetAccountTypesByAccountID(ByVal AccountGroupID As Integer) As DataTable

    Dim sql As String = "SELECT Account_Type_ID,Description FROM Tbl_Account_Type Where Account_Group_id= @AccountGroupID"
    Dim result As New DataTable()

    'Create a new connection each time. Really! Thanks to connection pooling, this is the way to go
    Using mcon As New SqlConnection(GetConnectionString()), _
          cmd As New SqlCommand(sql, mcon)

        'USE PARAMETERIZED QUERIES!!
        cmd.Parameters.Add("@AccountGroupID", SqlDbTypes.Int).Value = AccountGroupID

        'The Using block will make sure the connection is closed, even if an exception is thrown
        mcon.Open()
        Using rdr As SqlDataReader = cmd.ExecuteReader()
            result.Load(rdr)
        End Using
    End Using
    Return result
End Function


Private Sub cboAccountGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles cboAccountGroup.SelectedIndexChanged
    If mblnAccountGroupFirstLoad Then Exit Sub
    mblnAccountTypeFirstLoad = False

    ' clearDataEntries()

    Dim AccountTypes As DataTable = GetAccountTypesByAccountID(Integer.Parse(cboAccountGroup.SelectedValue))

    cboAccountType.DisplayMember = AccountTypes.Columns(1).ColumnName
    cboAccountType.ValueMember   = AccountTypes.Columns(0).ColumnName
    cboAccountType.DataSource = AccountTypes 'Set datasoure LAST

End Sub