Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I have 3 checkboxes, two(opOne,defOne) of which are in a datagridview. Im get an error "Specified cast is not valid" error.If you click on ckbOpOne it is supposed to change the value on opOne depending on the value of defOne. The code below works if i change all defOne states to true and then click ckbOne. Some rows already had defOne set to true but it does not work?

EDIT 1:
Any row in defOne could be True or False(Which is predefined in the db) once you click ckbOne it looks at defOne to see if its True or False, if it true it changes opOne to True.

What should happen:
Initial state = [] ([],[T])
User clicks ckbOne = [T] ([],[T]) = [T] ([T],[T])

C#



What I have tried:

C#
if (ckbOpOne.Checked)
{
   foreach (DataGridViewRow row in dgv.Rows)
   {
       DataGridViewCheckBoxCell opOne = (DataGridViewCheckBoxCell)row.Cells[1];
       DataGridViewCheckBoxCell defOne = (DataGridViewCheckBoxCell)row.Cells[7];
        opOne.Value = (defOne.Value == null ? false : (bool)defOne.Value);


    }
}
else
{
    foreach (DataGridViewRow row in dgv.Rows)
    {
         DataGridViewCheckBoxCell opOne = (DataGridViewCheckBoxCell)row.Cells[1];
         DataGridViewCheckBoxCell defOne = (DataGridViewCheckBoxCell)row.Cells[7];
         opOne.Value = false;
         //!(defOne.Value == null ? false : (bool)defOne.Value);


    }
}


EDIT 2:
I have got it kind of working.
C#
if (checkBox1.Checked)
            {
                foreach (DataGridViewRow row in dgv.Rows)
                {

                    if (!(Convert.IsDBNull(row.Cells[6].Value)) && Convert.ToBoolean(row.Cells[6].Value) == true)
                    {
                        DataGridViewCheckBoxCell opOne = (DataGridViewCheckBoxCell)row.Cells[0];
                        opOne.Value = !(opOne.Value == null ? false : (bool)opOne.Value); 
                    }
                }
            }
            else
            {
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    DataGridViewCheckBoxCell opOne = (DataGridViewCheckBoxCell)row.Cells[0];
                    DataGridViewCheckBoxCell defOne = (DataGridViewCheckBoxCell)row.Cells[6];
                    if(Convert.ToBoolean(row.Cells[6].Value) == true && Convert.ToBoolean(row.Cells[0].Value) == true)
                    {
                        opOne.Value = false;
                    }
                }
            }
}
Posted
Updated 1-Jul-16 0:31am
v4
Comments
Karthik_Mahalingam 1-Jul-16 4:36am    
make sure that 2nd and 8th column is of type checkbox.
BEBE2011 1-Jul-16 4:46am    
Yep, checked that. they are.
Karthik_Mahalingam 1-Jul-16 4:51am    
i have tested your code it works without exception.
under which scenario you are getting this error
BEBE2011 1-Jul-16 4:55am    
I will update my question with better example
Karthik_Mahalingam 1-Jul-16 4:57am    
ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900