//
You're reading...
Controls, DatagridView

Populate DataGridViewCheckBoxColumn at Runtime

This article shows how to populate the CheckBoxColumn of the DataGridView at runtime from the database. This code demonstrate that a datasource has been bind to the datagridview and using the datagridview’s “DataPropertyName” to the checkbox column. It will automatically populate the column with the desired values.

Here is the code:

Dim dt As DataTable = SqlSupport.SelectRecords("select * from TableA")
DataGridView1.AutoGenerateColumns = False        ' make it false when you are generating column by your own
DataGridView1.DataSource = dt                    ' setting data source an important one

' Creating a new column with name isActive with column type DataGridViewCheckBoxColumn
DataGridView1.Columns.Add(New DataGridViewCheckBoxColumn() With {.HeaderText = "IsActive", .Name = "IsActive"})

' Specified database column name to bind with grdi column using "DataPropertyName" property
DirectCast(DataGridView1.Columns("IsActive"), DataGridViewCheckBoxColumn).DataPropertyName = "IsActive"

Above code will create a new column in datagridview and display values with either checked or unchecked.

NOTE: you must have “IsActive” Column with type “bit” in your table

About Shahan Ayyub

Shahan is a Microsoft Certified Professional of .Net. Mostly self taught programmer and a quick learner, who just love to code and always eager to find unique solutions to the common problems. Currently working as an iOS developer.

Discussion

2 thoughts on “Populate DataGridViewCheckBoxColumn at Runtime

  1. Am not sure that I followed, could not comprehend; but what I know for certain is you are a brilliant coder.
    Dr. Youniss

    Posted by youniss | April 18, 2012, 7:47 pm

Leave a comment

Pages

LinkedIn Profile

Volunteer Expert at EE

Code Project Profile

My Code project

Stackoverflow Profile

profile for NeverHopeless on Stack Exchange, a network of free, community-driven Q&A sites

BrainBench Certified

Freelancer

Elance Testimonials