Add a CheckBox to DataTable and DataGridView

To add a Column with CheckBox, in a DataGridView, do the following:
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "chkBxName";
checkColumn.HeaderText = "Check Box";
checkColumn.ReadOnly = false;

// if the datagridview is resized (on form resize) 
// the checkbox won't take up too much; value is 
// relative to the other columns' fill values
checkColumn.FillWeight = 10;

It’s even simpler to add the CheckBox column to a DataTable and then, set it as a source to a DataGridView. Just set the type of a DataColumn to Boolean.
DataTable dt = new DataTable();

//this will show checkboxes
dt.Columns.Add(new DataColumn("Select", typeof(bool)));
dt.Columns.Add(new DataColumn("SomeText", typeof(string)));

dataGridView1.DataSource = dt;

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.