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

Submitted by: 
Visitors have accessed this post 149 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
  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#.

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.

  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.

  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.

  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;
  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.

  2. private void loaddata()
  3. {
  4. try
  5. {
  6. sql = "SELECT * FROM `profile`";
  8. con.Open();
  9. cmd = new MySqlCommand();
  10. da = new MySqlDataAdapter();
  11. dt = new DataTable();
  14. cmd.Connection = con;
  15. cmd.CommandText = sql;
  16. da = new MySqlDataAdapter();
  17. da.SelectCommand = cmd;
  18. dt = new DataTable();
  19. da.Fill(dt);
  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.

  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.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.