从DataGridViewCheckBoxCell获取值

Bry*_*can 5 c# winforms

我工作的一个DataGridViewListingGrid试图激活/停用已被"选中"的用户在任何DataGridViewCheckBoxCell这是内部的DataGridViewCheckBoxColumn.

这是我尝试这样做的方式:

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);

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,当你检查a时DataGridViewCheckBoxCell,单元格的值是true对的吗?但它不允许我将值转换为bool然后比较它,给我一个无效的强制转换异常.

Dev*_*per 17

尝试:

DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;

    if (Convert.ToBoolean(chkchecking.Value) == true)
{
}
Run Code Online (Sandbox Code Playgroud)

要么

DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;

        if ((bool)chkchecking.Value == true)
    {
    }
Run Code Online (Sandbox Code Playgroud)

  • 第一个解决方案对我有用,第二个则没有。可能是什么问题呢?只是好奇。 (2认同)