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

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

infi021

IoT Security Specialist
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
207
Likes
56
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. INSERT

    INTO

    `profile`

    (

    `id`

    ,

    `name`

    ,

    `gender`

    ,

    `email`

    )

    VALUES


  9. (

    1

    ,

    'Jaison Joy'

    ,

    'Male'

    ,

    '[email protected]'

    )

    ,
  10. (

    2

    ,

    'Vini'

    ,

    'Female'

    ,

    '[email protected]'

    )

    ,
  11. (

    3

    ,

    'John'

    ,

    'Male'

    ,

    '[email protected]'

    )

    ,
  12. (

    4

    ,

    'Test'

    ,

    'Female'

    ,

    'Test'

    )

    ,
  13. (

    5

    ,

    'test2'

    ,

    'Male'

    ,

    'Test2'

    )

    ;

Creating Application

Step 1

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

Step 2

Do the form just like shown below.
2019-09-10.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

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

  1. MySqlConnection con =

    new

    MySqlConnection(

    "server=localhost;user id=root;password=;database=profile;sslMode=none"

    )

    ;
  2. MySqlCommand cmd;
  3. MySqlDataAdapter da;
  4. DataTable dt;
  5. String

    sql;

Step 6

Create a method for creating a checkbox column.

  1. private

    void

    createCheckbox(

    )
  2. {
  3. //'set a variable as a checkbox column in the DataGridView
  4. DataGridViewCheckBoxColumn chkbox =

    new

    DataGridViewCheckBoxColumn(

    )

    ;
  5. //'set the width of the column in the DataGridView
  6. chkbox.

    Width

    =

    40

    ;

  7. //'Adding the checkbox column in the DataGridView
  8. dataGridView1.

    Columns

    .

    Add

    (

    chkbox)

    ;
  9. //'set the rows header to invisible
  10. dataGridView1.

    RowHeadersVisible

    =

    false

    ;
  11. }

Step 7

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

  1. private

    void

    loaddata(

    )
  2. {
  3. try
  4. {
  5. sql =

    "SELECT * FROM `profile`"

    ;

  6. con.

    Open

    (

    )

    ;
  7. cmd =

    new

    MySqlCommand(

    )

    ;
  8. da =

    new

    MySqlDataAdapter(

    )

    ;
  9. dt =

    new

    DataTable(

    )

    ;


  10. cmd.

    Connection

    =

    con;
  11. cmd.

    CommandText

    =

    sql;
  12. da =

    new

    MySqlDataAdapter(

    )

    ;
  13. da.

    SelectCommand

    =

    cmd;
  14. dt =

    new

    DataTable(

    )

    ;
  15. da.

    Fill

    (

    dt)

    ;

  16. //'adding a checkbox column in the datagridview
  17. createCheckbox(

    )

    ;
  18. //'display the data from the database to the datagridview
  19. dataGridView1.

    DataSource

    =

    dt;
  20. }
  21. catch

    (

    Exception ex)
  22. {
  23. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  24. }
  25. finally
  26. {
  27. con.

    Close

    (

    )

    ;
  28. da.

    Dispose

    (

    )

    ;
  29. }
  30. }

Step 8

Execute the loaddata()

method in the first load of the form.

  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. loaddata(

    )

    ;
  4. }

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