我正在开发一个名为ListingGrid的DataGridView,试图激活/停用在DataGridViewCheckBoxColumn内部的任何DataGridViewCheckBoxCell上被“选中”的用户。
这就是我尝试做的事情:
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,然后进行比较,从而抛出一个无效的强制转换异常。
发布于 2012-11-30 02:58:21
尝试:
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)
{
}发布于 2017-08-02 01:12:36
我认为== true没有什么用处,你必须检查你的单元是否不是DBNull:
if (chkchecking.Value != DBNull.Value)发布于 2018-05-25 18:51:28
我通常是这样做的bool value = (short)chkchecking.Value == 1
https://stackoverflow.com/questions/13632536
复制相似问题