从combo-box1中选择的值填充Combo-box2

时间:2016-03-15 13:37:30

标签: sql database vb.net combobox oledb

First Combo-box有学生管理员号码。选择一个数字后,下一个组合框应显示该学生的课程列表。但是,当选择一个值时,第二个组合框不会显示任何内容。

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

任何帮助我朝着正确方向前进的帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

通过将Combobox的DataSource属性设置为DataTable,将DataTable绑定到ComboBox怎么样?

编辑:我已经测试了您的代码,如果您更改

,它可以正常工作
Dim StudentAdminNo As Integer = CBStudent.SelectedValue 

Dim StudentAdminNo As Integer = CBStudent.SelectedItem