Friday, June 1, 2007

How to get from a DataGridViewCheckBoxCell the CheckBoxValue?

Often you only need to bind a list and the user need to check what it desire. To do it, simply bind your list to a grid and add an unbound colum with the type of DataGridViewCheckBoxColumn.

The example below contain grid of CheckBoxColumn and an TextBoxColumn that contain an ID.

List lstIdModuleToAdd = new List();
DataGridViewRow row;
DataGridViewCheckBoxCell cell;
for (int i = 0; i < this.chkModuleList.Rows.Count;i++ )//Loop all grid rows
{
row = this.chkModuleList.Rows[i];
cell = row.Cells["chkAddThisModule"] as DataGridViewCheckBoxCell;
if ((null != cell && null != cell.Value && true == (bool)cell.Value))//If checkbox == true
{
lstIdModuleToAdd.Add(Convert.ToInt32(row.Cells["Id"].Value));
}
}

1 comment:

Unknown said...

Smiling is definitely one of the best beauty remedies. If you have a good sense of humor and a good approach to life, that's beautiful. See the link below for more info.


#approach
www.ufgop.org