I am working on a DataGridView
called ListingGrid
trying to activate / deactivate users that have been "checked" on any DataGridViewCheckBoxCell
that is inside the DataGridViewCheckBoxColumn
.
This is the way im trying to do that :
foreach (DataGridViewRow roow in ListingGrid.Rows)
{
if ((bool)roow.Cells[0].Value == true)
{
if (ListingGrid[3, roow.Index].Value.ToString() == "True")
{
aStudent = new Student();
aStudent.UserName = ListingGrid.Rows[roow.Index].Cells[2].Value.ToString();
aStudent.State = true;
studentList.Add(aStudent);
}
}
}
As far as I get, when you check a DataGridViewCheckBoxCell
, the value of the cell is true
right? But it is not allowing me to convert the value to bool and then compare it, throwing me an invalid cast exception.
You should use Convert. ToBoolean() to check if dataGridView checkBox is checked.
try:
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chkchecking.Value) == true)
{
}
or
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if ((bool)chkchecking.Value == true)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With