//
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

Advertisements

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: