I have a List<Car> objects that have a bool property named Marked.
I want to check / uncheck the Cell corresponding to this property, in a way that only one Car can be selected at the same time.
I want that the value of this property is updated in the bank.
Sample table:
| Car Name | Marked |
|---|---|
| a | |
| b |
The problem is I can't check the state of CheckBox Cells.
This is what I tried, but it's not working:
Example 1:
DataGridViewCheckBoxCell dataGridViewCheckBoxCell = DataGridView1.Rows[e.RowIndex].Cells["Marked"] as DataGridViewCheckBoxCell;
if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == true)
{
//some code
}
else if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == false)
{
//some code
}
Example 2:
foreach (DataGridViewRow row in DataGridView1.Rows)
{
DataGridViewCheckBoxCell chk =(DataGridViewCheckBoxCell)row.Cells["Marked"];
if (chk.Value == chk.TrueValue)
{
chk.Value = chk.FalseValue;
}
else
{
chk.Value = chk.TrueValue;
}
}
how can I do it?

It is unclear “where” the first code snippet is called from so I will focus on the second snippet of code.
One of the problems you will have comes from the
ifstatement…This condition
chk.Value == chk.TrueValuemay not throw an error, HOWEVER… this is checking two different OBJECTS…chk.Valueis an OBJECT as ischk.TrueValue… so it makes sense that the two “different” OBJECTS would NEVER be equal.One way to solve this is to simply “cast” those OBJECTS to
boolvalues like…HOWEVER the above line of code will ONLY work “IF”… the check box columns
TrueValueANDFalseValueproperties have been SET like…IF you have NOT set the Check box column’s two properties
TrueValueANDFalseValuelike above… then the Check box columnsTrueValueandFalseValuewill benulland this will cause the line of code …if ((bool)chk.Value == (bool)chk.TrueValue) …to throw an exception sincechk.TrueValueisnulland the cast to aboolwill throw anullexception.Therefore, if you DO SET the Check box columns
TrueValueandFalseValuethen you will have to “cast” the objects toboolvalues in order for the comparison to work as shown above.Given all this… you may want to re-consider using the Check box columns
TrueValueandFalseValueas the intended purpose of those properties is to allow you to “change” the actualtrueandfalsevalues to something else that is not necessarily atrueorfalsevalue.It is unimportant how you would use these properties in that context as it appears very clear that in this context… setting the Check box columns
TrueValueandFalseValueproperties totrueandfalseis somewhat superfluous and creates more work for you.So in this case, I suggest you completely drop using the Check box columns
TrueValueandFalseValueproperties.If you want to ensure that only ONE check box is checked in the column… then you could wire up the grids
CellContentClickevent. If the check box value changed, then simply “uncheck” all the cells in that column.Fortunately, the grids CellContentClick will fire “before” it actually sets the check box cell, so unchecking them all works out fine for both checked and unchecked states. Crude yes, but it should work.
I hope this makes sense and helps.