2012-03-30 76 views
0

我有一個使用一組三個組合框的應用程序。 我已經成功地填充每個下拉菜單,具體取決於前一個的選定值 唯一的問題是,由於數據到了數據庫,第一個組合框中的兩個選定值可能在數據庫中有兩個不同數量的項目。因此第二組合框應該與這樣的數據,所述第一組合框的selectedIndex_changed nethod被調用Visual Basic 2008中的動態下拉菜單

如果例如,第一組合框的第一項具有在第二個組合箱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 

請協助。

回答

1

我會改變的代碼了幾分看起來更像是這樣的:

'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 
+0

感謝它幫助我獲得解決方案 – Tamseyc 2012-04-02 08:27:34