Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from DataGridViewCheckBoxCell

Tags:

c#

winforms

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.

like image 658
Bryan Arbelo - MaG3Stican Avatar asked Nov 29 '12 18:11

Bryan Arbelo - MaG3Stican


People also ask

How to get dataGridView checkBox checked in c#?

You should use Convert. ToBoolean() to check if dataGridView checkBox is checked.


1 Answers

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)
    {
    }
like image 101
Developer Avatar answered Nov 08 '22 11:11

Developer