vendredi 13 octobre 2017

Search through dataGridView using multiple checkboxes

I have started to create an app which will eventually give someone the option to select multiple year groups and collate a list of email addresses.

So far I have imported an excel file into a dataGridView box and managed to filter out items however, lets say for example i filter 'Year 7'. As soon as i select 'Year 8' the previous search disappears. Sometimes I will need to select Year 7, 8 and 9 together.

This is what i have so far.

Initially i thought i could create an if statement but it seems that it just searches whatever the last thing in the statement is.

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




Aucun commentaire:

Enregistrer un commentaire