vb.net ListBox中的多选

时间:2018-02-21 14:55:34

标签: sql vb.net

我在listBox中有一个学生姓名列表,(studentList)我点击框中的名字并获取所有学生的详细信息,即名称,课程,科目等。然后代码从数据库中获取详细信息(在我的情况下,它是访问)然后在数据网格视图中显示它。 如果我只从一个(或所有)列表框中选择一个项目,代码工作正常。我的问题是,我可以为每个LitsBox选择多个项目。我知道我可以使用SelectedMode属性来允许突出显示但不会绘制所需的来自数据库的数据。这是我使用的代码vb.10

    `Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles Button1.Click
    Dim con As New OleDb.OleDbConnection 

    Dim dbProvider As String
    Dim dbSource As String

    Dim ds As New DataSet

    Dim tables As DataTableCollection = ds.Tables

    Dim source1 As New BindingSource()

    Dim da As New OleDb.OleDbDataAdapter        

    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"

    dbSource = "Data Source = C:\Documents and Settings\Desktop  \studentmarks.accdb"
    con.ConnectionString = dbProvider & dbSource

    con.Open()

    Dim isFirstColumn As Boolean = True
    Dim student As String = ""
    Dim course As String = ""
    Dim grade As String = ""


    Dim x As String = studentList.Text
    Dim y As String = courseList.Text
    Dim z As String = gradeList.Text     


    Dim defaultSQL As String = "SELECT * FROM studentfile "       

    If studentList.SelectedIndex > -1 Then 
        If isFirstColumn Then
            student = "WHERE student = '" & x & "' " 
        Else
            student = "AND student = '" & x & "' "
        End If
        isFirstColumn = False
    End If

    If courseList.SelectedIndex > -1 Then 
        If isFirstColumn Then
            course = "WHERE course = '" & y & "' " 
        Else
            course = "AND course = '" & y & "' "
        End If
        isFirstColumn = False
    End If       

    If gradeList.SelectedIndex > -1 Then 
        If isFirstColumn Then
            grade = "WHERE grade = '" & z & "' " 
        Else
            grade = "AND grade = '" & z & "' "
        End If
        isFirstColumn = False
    End If       

    Dim sql As String = defaultSQL & student & course & grade

    da = New OleDb.OleDbDataAdapter(sql, con)

    da.Fill(ds, "topclass")


    Dim view1 As New DataView(tables(0))

    source1.DataSource = view1
    DataGridView1.DataSource = view1
    DataGridView1.Refresh()
    DataGridView1.DataSource = view1

    DataGridView1.Refresh()

    Dim cnt As Integer

    cnt = DataGridView1.Rows.Count

    TextBox1.Text = cnt - 1

    Dim dayclass As String = TextBox1.Text
    TextBox8.Text = dayclass
    con.Close()


End Sub` 
非常感谢 灰色

1 个答案:

答案 0 :(得分:0)

使用.SelectedItems(和很多jiggery一起使用where子句....你应该可以使用它来为EITHER在所有三个列表框中选择多个选择,而不是从任何一个选择任何内容他们也是......

我唯一的问题就是你是否想要所有的'因为这意味着如果你选择了两个学生,你就不会得到任何结果......因为没有两个学生是一样的对?除非你选择两个Dave,否则它将同时返回。例如!

可能会建议更改一些“或”或查看最终结果可能是什么?无论哪种方式发表评论,如果需要任何进一步的帮助,请告知我们

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim con As New OleDb.OleDbConnection

    Dim dbProvider As String
    Dim dbSource As String

    Dim ds As New DataSet

    Dim tables As DataTableCollection = ds.Tables

    Dim source1 As New BindingSource()

    Dim da As New OleDb.OleDbDataAdapter

    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"

    dbSource = "Data Source = C:\Documents and Settings\Desktop  \studentmarks.accdb"
    con.ConnectionString = dbProvider & dbSource

    con.Open()


    Dim student As String = ""
    Dim course As String = ""
    Dim grade As String = ""

    Dim defaultSQL As String = "SELECT * FROM studentfile "
    Dim WhereClause As String = ""
    Dim StudentCourseGrade As String
    'Students---------------------------------------------
    For Each stu In studentList.SelectedItems
        StudentCourseGrade = "(student='" & stu & "'"
        For Each crs In courselist.SelectedItems
            StudentCourseGrade = StudentCourseGrade & " AND course = '" & crs & "'"
            For Each grd In gradeList.SelectedItems
                StudentCourseGrade = StudentCourseGrade & " AND grade = '" & crs & "'"
            Next
        Next
        StudentCourseGrade = StudentCourseGrade & ")"

        If WhereClause.Length = 0 Then
            WhereClause = "WHERE " & StudentCourseGrade
        Else
            WhereClause = " OR " & StudentCourseGrade
        End If
    Next
    'Students---------------------------------------------

    Dim SQL As String = defaultSQL & WhereClause

    da = New OleDb.OleDbDataAdapter(sql, con)

    da.Fill(ds, "topclass")


    Dim view1 As New DataView(tables(0))

    source1.DataSource = view1
    DataGridView1.DataSource = view1
    DataGridView1.Refresh()
    DataGridView1.DataSource = view1

    DataGridView1.Refresh()

    Dim cnt As Integer

    cnt = DataGridView1.Rows.Count

    TextBox1.Text = cnt - 1

    Dim dayclass As String = TextBox1.Text
    TextBox8.Text = dayclass
    con.Close()


End Sub

H个 鸡