我工作的一个DataGridView
叫ListingGrid
试图激活/停用已被"选中"的用户在任何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)