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;

dataGridView1.Columns.Add(checkColumn);
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;
Advertisements