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
Am not sure that I followed, could not comprehend; but what I know for certain is you are a brilliant coder.
Dr. Youniss
It is great to see you here. thanks for your contribution.