从组合框1中选择的值填充组合框2
问题描述:
第一个组合框具有学生管理员号码。当选择一个数字时,下一个组合框应该显示该学生的班级列表。但是,当选择一个值时,第二个组合框不显示任何内容。从组合框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
任何帮助推动我在正确的方向将不胜感激。
答
通过将Combobox的DataSource属性设置为DataTable,将DataTable绑定到ComboBox怎么样?
编辑:我测试你的代码,如果你改变
Dim StudentAdminNo As Integer = CBStudent.SelectedValue
到
Dim StudentAdminNo As Integer = CBStudent.SelectedItem
CBClass和CBStudent是组合框 –
答案更新它工作正常。希望它有效 – noidea