Get value from DataGridViewCheckBoxCell

I am working on a DataGridView called ListingGrid , trying to activate / deactivate users who have been "tagged" on any DataGridViewCheckBoxCell that is inside the DataGridViewCheckBoxColumn .

This is exactly how I am trying to do this:

 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 understand, when you check the DataGridViewCheckBoxCell , is the value of the true cell correct? But this does not allow me to convert the value to bool, and then compare it, throwing me an invalid exception.

+4
source share
2 answers

to 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) { } 
+13
source

I think == true not useful, and you should check if your cell is DBNull :

 if (chkchecking.Value != DBNull.Value) 
-1
source

All Articles