2016-03-15 254 views
0

第一個組合框具有學生管理員號碼。當選擇一個數字時,下一個組合框應該顯示該學生的班級列表。但是,當選擇一個值時,第二個組合框不顯示任何內容。從組合框1中選擇的值填充組合框2

AdminNo存儲在數據庫中的兩個學生ClassEnrollment表所示。

Imports System.Data.OleDb 

Public Class AddReport 
    Dim cn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\ProjectDatabase.mdb") 
    Dim daa As New OleDbDataAdapter() 
    Dim dt As New DataTable() 

Private Sub btnReturn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReturn.Click 
    Me.Close() 
    Startup.Show() 
End Sub 

Private Sub AddReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    StudentNameAdd() 
End Sub 

Sub StudentNameAdd() 
    cn.Open() 
    daa.SelectCommand = New OleDbCommand("Select AdminNo from Student", cn) 
    daa.Fill(dt) 
    CBStudent.Items.Clear() 
    Dim r As DataRow 
    For Each r In dt.Rows 
     CBStudent.Items.Add(r(0).ToString) 
    Next 
    cn.Close() 

End Sub 

Private Sub CBStudent_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CBStudent.SelectedIndexChanged 
    dt.Reset() 
    Dim StudentAdminNo As Integer = CBStudent.SelectedValue 
    cn.Open() 
    daa.SelectCommand = New OleDbCommand("Select ClassCode from ClassEnrollment Where AdminNo=" & StudentAdminNo, cn) 
    daa.Fill(dt) 
    CBClass.Items.Clear() 
    Dim r As DataRow 
    For Each r In dt.Rows 
     CBClass.Items.Add(r(0).ToString) 
    Next 
    cn.Close() 
End Sub 

任何幫助推動我在正確的方向將不勝感激。

回答

0

通過將Combobox的DataSource屬性設置爲DataTable,將DataTable綁定到ComboBox怎麼樣?

編輯:我測試你的代碼,如果你改變

Dim StudentAdminNo As Integer = CBStudent.SelectedValue 

Dim StudentAdminNo As Integer = CBStudent.SelectedItem 
+0

CBClass和CBStudent是組合框 –

+0

答案更新它工作正常。希望它有效 – noidea