根据组合框选择的项目填充数据网格视图,这取决于数据库中另一个选择的组合框

fill datagridview based on combobox selected item which depends on another selected combobox from database

我正在创建一个添加学生 windows 表单,它包含两个组合框。第一个包含学生的学期,第二个包含基于学期值的课程代码。我面临的问题是我也有一个 datagridview,我想用 table 课程代码中包含的学生数据填充它,一旦我单击学期组合框,课程代码组合框的值就会更改,尽快随着它的变化,我想加载 datagridview 的数据,但它显示的错误基本上是无法将数据行转换为字符串,但是当我使用 ComboBox1.ValueMember 并在单击按钮时加载 datagridview 时它工作正常。但是当我在组合框更改上加载 datagridview 时它显示错误。

这段代码工作正常

Imports System.Data.OleDb
Public Class AddStudent
    Dim con As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Students.accdb")
    Dim con1 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Users.accdb")
    Private Sub AddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        con.Open()
        Dim cmd As New OleDbCommand("Select stud_roll From " + ComboBox1.SelectedValue + " where stud_roll = @roll1", con)
        cmd.Parameters.AddWithValue("roll1", TextBox2.Text)
        Dim myreader As OleDbDataReader = cmd.ExecuteReader
        If myreader.Read() Then
            con.Close()
            MessageBox.Show("Student Inserted before")
        Else
            con.Close()
            Dim cmd1 As New OleDbCommand("Insert into " + ComboBox1.SelectedValue + "(stud_roll,stud_name,date_of_birth,course,gender,mobile_no,semester) Values(@roll,@name,@dob,@course,@gender,@mobile,@semester)", con)
            cmd1.Parameters.AddWithValue("roll", Convert.ToInt32(TextBox2.Text))
            cmd1.Parameters.AddWithValue("name", TextBox1.Text)
            cmd1.Parameters.AddWithValue("dob", DateTimePicker1.Value.Date)
            cmd1.Parameters.AddWithValue("course", ComboBox1.SelectedValue)
            cmd1.Parameters.AddWithValue("gender", ComboBox2.SelectedItem)
            cmd1.Parameters.AddWithValue("mobile_no", MaskedTextBox1.Text)
            cmd1.Parameters.AddWithValue("semester", Convert.ToInt32(ComboBox3.SelectedItem))
            con.Open()
            cmd1.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("Records inserted successfully")
        End If
        con.Open()
        Dim cmd3 As New OleDbCommand("Select stud_roll, stud_name, date_of_birth, course, gender,mobile_no, semester From " + ComboBox1.SelectedValue + "", con)
        Dim da As New OleDbDataAdapter
        da.SelectCommand = cmd3
        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt
        con.Close()
    End Sub

    Private Sub Label8_Click(sender As Object, e As EventArgs) Handles Label8.Click
        AddTeacher.Show()
    End Sub

    Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
        con1.Open()
        Dim cmd As New OleDbCommand("Select course_code From courses Where semester= " + ComboBox3.SelectedItem + "", con1)
        Dim da As New OleDbDataAdapter
        da.SelectCommand = cmd
        Dim dt As New DataTable
        dt.Clear()
        da.Fill(dt)
        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "course_code"
        ComboBox1.ValueMember = "course_code"
        con1.Close()
    End Sub

    Private Sub Label14_Click(sender As Object, e As EventArgs) Handles Label14.Click
        Courses.Show()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub
End Class

当我像这样更改组合框后加载时,它显示错误:- System.InvalidCastException: 'Operator '+' is not defined for string "Select stud_name, stud_roll 来自”并键入 'DataRowView'。' 这是因为数据行视图类型转换

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    con.Open()
    Dim cmd3 As New OleDbCommand("Select stud_roll, stud_name, date_of_birth, course, gender,mobile_no, semester From " + ComboBox1.SelectedValue + "", con)
    Dim da As New OleDbDataAdapter
    da.SelectCommand = cmd3
    Dim dt As New DataTable
    dt.Clear()
    da.Fill(dt)
    DataGridView1.DataSource = dt
    con.Close()
End Sub 

请帮我解决这个问题。

1. 确保给控件起有意义的名字:这会让你的生活更轻松。 “ComboBox1”不是一个有意义的名称 ;)

2. 不要创建重复使用的数据库连接的单个实例:您应该创建连接对象,使用它,然后处理它。幕后有一些机制可以提高效率 (Connection Pooling). There is an example of how to do it in a previous .

3. 您不需要为 da.Fill(dt) 打开和关闭连接:它会自动完成。

4. 因为 ComboBox1.SelectedValue 来自数据表,所以您需要提取该数据行视图的列,例如:

Dim tableName = DirectCast(ComboBox1.SelectedItem, DataRowView).Row.Field(Of String)("course_code")
Dim cmd3 As New OleDbCommand("Select stud_roll, stud_name, date_of_birth, course, gender,mobile_no, semester From [" & tableName & "]", con)