How to Fillter DataGridView using Multiple CheckBox - BTS Au Maroc

vendredi 16 février 2018

How to Fillter DataGridView using Multiple CheckBox









Full code Source


Fillter DataGridView using Multiple CheckBox

in your visual studio, create new project and rename it with "FillterDataGridView" and at the form1.vb add one DataGridView, Two GroupBox, and five CheckBox, see images below :

How to Fillter DataGridView using Multiple CheckBox

Next, Add new Module, called "ModuleConnection.vb" for our connection to database using ODBC class, or you can crate using ADO.NET.

Source Code ModuleConnection.vb

Imports System.Data.Odbc ' we can use ODBC class to create a connection
Module ModuleConnections
    ' first you must have a database,
    ' and we will connect it using ODBC connection.
    ' i have a database (MySQL database) and has create ODBC connection
    Public connection As OdbcConnection
    Public Sub connect()
        Try
            connection = New OdbcConnection("DSN=k13new;MultipleActiveResultSets=True")
            If connection.State = ConnectionState.Closed Then
                ' open connection
                connection.Open()
            End If
        Catch ex As Exception
            MsgBox("Connection failed!")
        End Try
    End Sub
End Module

Source Code Fillter Data (Form1.vb)

Imports System.Data.Odbc
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' open connection
        connect()
        Dim da As New OdbcDataAdapter("SELECT * FROM siswa", connection)
        Dim ds As New DataSet ' we will use dataset
        da.Fill(ds, "siswa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "siswa"
        da.Dispose()
        connection.Close()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim query As String = ""
        Dim query2 As String = ""
        Dim c1check As Boolean
        Dim c2check As Boolean

        Dim myList As New List(Of String)
        For Each cb As CheckBox In GroupBox1.Controls.OfType(Of CheckBox)
            ' we can know if checkbox in GroupBox1 is checked or not
            If cb.Checked Then
                c1check = True
                ' get the text of checkbox that checked by user
                myList.Add(cb.Text)
            End If
        Next

        ' now , add the same function to get value from groupbox 2
        Dim myList2 As New List(Of String)
        For Each cb2 As CheckBox In GroupBox2.Controls.OfType(Of CheckBox)
            ' we can know if checkbox in GroupBox2 is checked or not
            If cb2.Checked Then
                c2check = True
                ' get the text of checkbox that checked by user
                myList2.Add(cb2.Text)
            End If
        Next

        If c1check Then
            ' if checkbox in groupbox1 is checked, we will create this query to our database
            query = "SELECT * FROM siswa WHERE kelas='" & String.Join("' or kelas=" + "'", myList) + "'"
        End If
        If c2check Then
            ' if checkbox in groupbox2 is checked, we will create this query to our database
            query = "SELECT * FROM siswa WHERE jeniskelamin='" & String.Join("' or jeniskelamin=" + "'", myList2) + "'"
        End If
        If c1check And c2check Then
            ' if checkbox in GorupBox 1 and 2 is checked, we will create this query to our database
            query = "SELECT * FROM siswa WHERE (kelas='" & String.Join("' or kelas=" + "'", myList) + "') AND (jeniskelamin='" & String.Join("' or jeniskelamin=" + "'", myList2) + "')"

        End If

        ' open connection
        connect()
        Dim da As New OdbcDataAdapter(query, connection)
        Dim ds As New DataSet

        da.Fill(ds, "siswa")
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "siswa"
        da.Dispose()
        connection.Close()
    End Sub
End Class

Aucun commentaire:

Enregistrer un commentaire

Pages