views:

1511

answers:

2

Hello,

I have a problem which I can't seem to solve. I have a bound a data table to a datagridview, this datatable has a column called "Status" which is of type boolean. I can set the value to true or false just fine for via code. However, I can't figure out how to check and see if the a give row is already checked or not. This is the code I am trying to use and it keeps saying the "The Specified Cast Is Invalid." Any help would be appreciated.

 if (rowIndex >= 0)
            {
                DataGridViewCheckBoxCell CbxCell = (DataGridViewCheckBoxCell)dgv_Scan.Rows[rowIndex].Cells["Status"];

                if ((bool)CbxCell.Value == true)
                {
                    //Do stuff
                }
                else
                {
                    //Do Stuff
                }
            }

Thanks, Nathan

A: 

I have no prior experience in this, but I guess you should check the value of the column or property.

Try to have a look to this example:

http://programmingwithstyle.blogspot.com/2007/06/how-to-get-from-datagridviewcheckboxcel.html

Roberto Aloi
+1  A: 

CbxCell.Value must be equal to DBNull.Value (your column can contain null values right?)

I would check for DBNull before casting:

if (!DBNull.Value.Equals(CbxCell.Value) && (bool)CbxCell.Value == true)
{
    //Do stuff
}
else
{
    //Do Stuff
}
najmeddine