我正在开发一个名为 ListingGrid
的 DataGridView
,试图激活/停用已在内部的任何 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);
}
}
}
据我所知,当您检查 DataGridViewCheckBoxCell
时,单元格的值为 true
对吗?但它不允许我将值转换为 bool 然后比较它,抛出一个无效的转换异常。
最佳答案
尝试:
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chkchecking.Value) == true)
{
}
或
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if ((bool)chkchecking.Value == true)
{
}
关于c# - 从 DataGridViewCheckBoxCell 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13632536/