Download Source Code -How to Display Records with CheckBox Column in DatagridView Using C#

Loading...

How to Display Records with CheckBox Column in DatagridView Using C#

Submitted by: 
janobe
Language: 
Visual Basic .NET
Visitors have accessed this post 4 times.

In this tutorial, I’m going to teach you How to Display Records with CheckBox Column in DatagridView Using C#. This program illustrates how to retrieve data from the database and will be displayed in the datagridview with a corresponding checkbox column. The checkbox column is auto-generated and it can be found in the first column of the datagridview.

Creating a Database

Go to localhost/phpMyAdmin the 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. INSERT INTO `profile` (`id`, `name`, `gender`, `email`) VALUES
  10. (1, 'Jaison Joy', 'Male', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>'),
  11. (2, 'Vini', 'Female', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>'),
  12. (3, 'John', 'Male', '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>'),
  13. (4, 'Test', 'Female', 'Test'),
  14. (5, 'test2', 'Male', 'Test2');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
ps1

Step 2

Do the form just like shown below.
figure 2

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.  
  2. using MySql.Data.MySqlClient;

Step 5

Create a connection between MySQL Database and C#. After that, Initialize all the classes and variables that are needed.

  1.  
  2. MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=profile;sslMode=none");
  3. MySqlCommand cmd;
  4. MySqlDataAdapter da;
  5. DataTable dt;
  6. String sql;

Step 6

Create a method for creating a checkbox column.

  1.  
  2. private void createCheckbox()
  3. {
  4. //'set a variable as a checkbox column in the DataGridView
  5. DataGridViewCheckBoxColumn chkbox = new DataGridViewCheckBoxColumn();
  6. //'set the width of the column in the DataGridView
  7. chkbox.Width = 40;
  8.  
  9. //'Adding the checkbox column in the DataGridView
  10. dataGridView1.Columns.Add(chkbox);
  11. //'set the rows header to invisible
  12. dataGridView1.RowHeadersVisible = false;
  13. }

Step 7

Create a method for displaying records with a checkbox column in the datagridview.

  1.  
  2. private void loaddata()
  3. {
  4. try
  5. {
  6. sql = "SELECT * FROM `profile`";
  7.  
  8. con.Open();
  9. cmd = new MySqlCommand();
  10. da = new MySqlDataAdapter();
  11. dt = new DataTable();
  12.  
  13.  
  14. cmd.Connection = con;
  15. cmd.CommandText = sql;
  16. da = new MySqlDataAdapter();
  17. da.SelectCommand = cmd;
  18. dt = new DataTable();
  19. da.Fill(dt);
  20.  
  21. //'adding a checkbox column in the datagridview
  22. createCheckbox();
  23. //'display the data from the database to the datagridview
  24. dataGridView1.DataSource = dt;
  25. }
  26. catch(Exception ex)
  27. {
  28. MessageBox.Show(ex.Message);
  29. }
  30. finally
  31. {
  32. con.Close();
  33. da.Dispose();
  34. }
  35. }

Step 8

Execute the loaddata() method in the first load of the form.

  1.  
  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4. loaddata();
  5. }

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.

Tags: 
CheckBoxColumn in DataGridview
CheckBoxColumn in vb.net
retrieving data in vb.net
MySQL Database

Add new comment

(Visited 1 times, 1 visits today)
Loading...