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])
What I have tried:
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;
}
}
EDIT 2:
I have got it kind of working.
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;
}
}
}
}