• We just launched and are currently in beta. Join us as we build and grow the community.

How to Filter Data in the DataGridview Using CheckBoxes in VB.Net

Sallomy

Data Correlation Specialist
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
96
Likes
128
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
In this tutorial, I will teach you how to filter the data in the datagridview using checkboxes in vb.net. This method has the ability to retrieve the data in the database and It will be displayed in the datagridview depending on the checked in a group of checkboxes. This procedure is very helpful if you are currently working on the inventory of products.

Creating a Database

Go to and create a new database named "tuts_persondb".
after that execute the query for creating a table and adding the data in the table.
  1. CREATE

    TABLE

    `tblperson`

    (
  2. `PersonID`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. `Fullname`

    varchar

    (

    90

    )

    NOT

    NULL

    ,
  4. `CivilStatus`

    varchar

    (

    30

    )

    NOT

    NULL


  5. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  6. --
  7. -- Dumping data for table `tblperson`
  8. --

  9. INSERT

    INTO

    `tblperson`

    (

    `PersonID`

    ,

    `Fullname`

    ,

    `CivilStatus`

    )

    VALUES


  10. (

    1

    ,

    'Janobe Sourcecode'

    ,

    'Single'

    )

    ,
  11. (

    2

    ,

    'Mark Lim'

    ,

    'Married'

    )

    ,
  12. (

    3

    ,

    'Jake Cueca'

    ,

    'Widow'

    )

    ;

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for visual basic.
creating_vb_25.png

Step 2

Do the form just like shown below.
fig1.png

Step 3

Add MySQL.Data.dll
Step 4

Press F7 to open the code editor. In the code editor, add a namespace to access MySQL

libraries

  1. using MySql.Data.MySqlClient;

Step 5

Establish a connection between visual basic and MySQL database. After that, declare all the classes and variables that are needed.

  1. Dim

    con As

    MySqlConnection = New

    MySqlConnection("server=localhost;user id =root;password=;database=tuts_persondb;sslMode=none"

    )
  2. Dim

    cmd As

    MySqlCommand
  3. Dim

    da As

    MySqlDataAdapter
  4. Dim

    dt As

    DataTable
  5. Dim

    sql As

    String

    = ""
  6. Dim

    stat_single As

    String

    = ""
  7. Dim

    stat_married As

    String

    = ""
  8. Dim

    stat_widow As

    String

    = ""

Step 6

Create a method for retrieving data in the database.

  1. Private

    Sub

    loadData(sql As

    String

    )
  2. Try
  3. con.Open

    ()
  4. cmd = New

    MySqlCommand
  5. da = New

    MySqlDataAdapter
  6. dt = New

    DataTable

  7. With

    cmd
  8. .Connection = con
  9. .CommandText = sql
  10. End

    With

  11. da.SelectCommand = cmd
  12. da.Fill(dt)

  13. DataGridView1.DataSource = dt

  14. Catch ex As

    Exception
  15. MsgBox(ex.Message)
  16. Finally
  17. con.Close

    ()
  18. da.Dispose()
  19. End

    Try
  20. End

    Sub

Step 7

Write the following code to display the data into the DataGridView in the first load of the form.

  1. Private

    Sub

    Form1_Load(sender As

    Object

    , e As

    EventArgs) Handles MyBase.Load
  2. sql = "SELECT * FROM `tblperson`"
  3. loadData(sql)
  4. End

    Sub

Step 7

Write the following code for filtering the data in the DataGridView using the checkboxes when it's checked.

  1. Private

    Sub

    chkSingle_CheckedChanged(sender As

    Object

    , e As

    EventArgs) Handles chkSingle.CheckedChanged, chkMarried.CheckedChanged, chkWidow.CheckedChanged


  2. If

    chkSingle.Checked Then
  3. stat_single = chkSingle.Text
  4. End

    If

  5. If

    chkMarried.Checked Then
  6. stat_married = chkMarried.Text
  7. End

    If

  8. If

    chkWidow.Checked Then
  9. stat_widow = chkWidow.Text
  10. End

    If


  11. sql = "SELECT * FROM `tblperson` WHERE `CivilStatus` in ('"

    & stat_single & "','"

    & stat_married & "','"

    & stat_widow & "')"
  12. loadData(sql)

  13. End

    Sub

The complete source code is included you can download it and run it on your computer.

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top