通过过滤搜索在datagridview中显示多个值

时间:2016-03-09 20:41:07

标签: mysql vb.net datagridview

我正在制作一个通过搜索课程来搜索学生信息的vb程序。例如,Ajax的课程是“BSCS”,DP的课程是“BSIT”,那么我将选择BSCS,所以Ajax必须只出现不是DP。结果显示在DataGridView

    myConnection.Open()
    Dim str As String
    str = "SELECT * FROM students WHERE (Course = '" & TextBox1.Text & "')"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    dr = cmd.ExecuteReader
    While dr.Read()
        FirstNameTextBox = dr("FirstName")
        MiddleNameTextBox = dr("MiddleName")
        LastNameTextBox = dr("LastName")
        AddressTextBox = dr("Address")
        CellphoneNumberTextBox = dr("CellphoneNumber")
        CourseTextBox = dr("Course")
    End While
    myConnection.Close()

1 个答案:

答案 0 :(得分:0)

根据您的代码,您应该在表单中添加新的DataGridView,并添加包含您选择的标题名称的列,并将代码替换为我的

        myConnection.Open()
        Dim str As String
        str = "SELECT * FROM students WHERE (Course = '" & TextBox1.Text & "')"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
        dr = cmd.ExecuteReader
        While dr.Read()
           DataGridView1.Rows.Add({dr("FirstName"),  dr("MiddleName"), dr("LastName"), dr("Address"), dr("CellphoneNumber"), dr("Course")})
        End While
        myConnection.Close()