Retrieving Data with CheckBox in VB.Net

In this tutorial, I will teach you how to retrieve data with checkbox in vb.net. This procedure is very useful if you want something to retrieve in the database that would then be displayed in the datagridview together with the checkboxes. If you want to find out how it’s done, check the instructions below.

Creating Database

Create a database and named it “profile” After that, execute the following query to create a table and insert some data in the table.
  1. CREATE TABLE `profile` (
  2. `id` int(100) NOT NULL,
  3. `name` varchar(100) DEFAULT NULL,
  4. `email` varchar(100) DEFAULT NULL,
  5. `photo` varchar(250) DEFAULT NULL,
  6. `joindate` date NOT NULL
  7. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  8.  
  9. --
  10. -- Dumping data for table `profile`
  11. --
  12.  
  13. INSERT INTO `profile` (`id`, `name`, `email`, `photo`, `joindate`) VALUES
  14. (1, 'Jaison Joy', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '<a href="http://jaisonjoy.com//image/man.png'" rel="nofollow">http://jaisonjoy.com//image/man.png'</a>, '2019-01-01'),
  15. (2, 'Vini', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '<a href="http://jaisonjoy.com/image/female.png'" rel="nofollow">http://jaisonjoy.com/image/female.png'</a>, '2019-01-15'),
  16. (3, 'John', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>', '<a href="http://jaisonjoy.com/image/male.png'" rel="nofollow">http://jaisonjoy.com/image/male.png'</a>, '2019-02-03'),
  17. (4, 'Test', 'Test', NULL, '2019-02-14'),
  18. (5, 'test2', 'Test2', NULL, '2019-02-14');

Creating Application

Step 1

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

Step 2

Add a DataGridView inside the form. It will look like this. ps2

Step 3

Open the code edit by pressing F7 on the keyboard. In the code view, add a namespace above the class to access MySQL Libraries.
  1. Imports MySql.Data.MySqlClient

Step 4

Create a connection between MySQL and Visual Basic 2015 and declare all the classes that are needed.
  1. Dim con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;password=;database=profile;sslMode=none")
  2. Dim cmd As MySqlCommand
  3. Dim da As MySqlDataAdapter
  4. Dim dt As DataTable
  5. Dim sql As String

Step 5

Create a Sub Procedure in creating a CheckeBox object.
  1. Private Sub CheckboxColumn()
  2. 'set a variable as a checkbox column in the DataGridView
  3. Dim chkbox As New DataGridViewCheckBoxColumn
  4. 'set the width of the column in the DataGridView
  5. With chkbox
  6. .Width = 30
  7. End With
  8.  
  9. With DataGridView1
  10. 'Adding the checkbox column in the DataGridView
  11. .Columns.Add(chkbox)
  12. 'set the rows header to invisible
  13. .RowHeadersVisible = False
  14. End With
  15. End Sub

Step 6

Do the following code for retrieving data in the database to display it inside the DataGridView with CheckBoxes in the first load of the form.
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2. Try
  3. sql = "SELECT * FROM `profile`"
  4. con.Open()
  5. cmd = New MySqlCommand()
  6. With cmd
  7. .Connection = con
  8. .CommandText = sql
  9. End With
  10. da = New MySqlDataAdapter()
  11. da.SelectCommand = cmd
  12. dt = New DataTable()
  13. da.Fill(dt)
  14.  
  15. 'adding a checkbox column in the datagridview
  16. CheckboxColumn()
  17. 'display the data from the database to the datagridview
  18. DataGridView1.DataSource = dt
  19.  
  20. Catch ex As Exception
  21. MsgBox(ex.Message)
  22. Finally
  23. con.Close()
  24. da.Dispose()
  25. End Try
  26. End Sub
For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment