使用多个复选框搜索dataGridView

时间:2017-10-13 20:50:01

标签: vb.net search checkbox import datagrid

我已经开始创建一个应用程序,最终会让某人选择多年组并整理一个电子邮件地址列表。

到目前为止,我已经将一个excel文件导入到dataGridView框中,并设法过滤掉了项目,例如,我可以说是过滤' 7年级'。我选择' 8年级'之前的搜索消失了。有时我需要一起选择7年级,8年级和9年级。

这是我到目前为止所做的。

最初我认为我可以创建一个if语句,但它似乎只是搜索语句中的最后一件事。

Public Class Form1
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "C:\Users\Lenovo\Desktop\Username finder\data.xlsx"
Dim dv As DataView


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try

        MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [usernames$]", MyConnection)

        dataSet = New System.Data.DataSet
        MyCommand.Fill(dataSet)
        DataGridView1.DataSource = dataSet.Tables(0)

        MyConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try

End Sub


Private Sub CheckBoxY7_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxY7.CheckedChanged

    ' dv = New DataView(dataSet.Tables(0), "Year = '7' ", "Surname Desc", DataViewRowState.CurrentRows)
    ' DataGridView1.DataSource = dv
End Sub

Private Sub CheckBoxY8_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxY8.CheckedChanged
    ' dv = New DataView(dataSet.Tables(0), "Year = '8' ", "Surname Desc", DataViewRowState.CurrentRows)
    ' DataGridView1.DataSource = dv
End Sub

2 个答案:

答案 0 :(得分:0)

每次勾选复选框时,DataView都会丢失上一个RowFilter。你想要做的是使用所有选中的CheckBox控件构建RowFilter。看一下这个例子:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try

        MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [usernames$]", MyConnection)

        dataSet = New System.Data.DataSet
        MyCommand.Fill(dataSet)
        DataGridView1.DataSource = dataSet.Tables(0)

        MyConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try

    CheckBoxY7.Tag = "7"
    CheckBoxY8.Tag = "8"
    'etc...
End Sub

Private Sub Year_CheckBox_CheckChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBoxY7.Checked, CheckBoxY8.Checked
  Dim row_filter As String = String.Join(" AND ", (From cb As CheckBox In {CheckBoxY7, CheckBoxY8} Where cb.Checked Select $"Year = '{cb.Tag.ToString()}'").ToArray())
  DataGridView1.DataSource = New DataView(dataSet.Tables(0), row_filter, "Surname Desc", DataViewRowState.CurrentRows)
End Sub

答案 1 :(得分:0)

感谢那些回复的人。

我已经通过向属性添加标记并编写for循环来完成解决方案。

    For Each ctrl In Panel1.Controls
        If (ctrl.GetType() Is GetType(CheckBox)) Then
            chkbox = ctrl
            If chkbox.Checked = True Then
                If FirstLoop Then
                    qstring = ctrl.Tag
                    FirstLoop = False
                Else
                    qstring = qstring & " or " & ctrl.Tag
                End If
            End If
        End If
    Next
    ResultBox.Text = qstring
    dv = New DataView(dataSet.Tables(0), qstring, "Surname Desc", DataViewRowState.CurrentRows)
    DataGridView1.DataSource = dv