本文介绍了从DataGridViewCheckBoxCell获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我工作的一个 DataGridView的
名为 ListingGrid
试图激活/停用已用户选中上任何 DataGridViewCheckBoxCell
这就是 DataGridViewCheckBoxColumn
。
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
.
这是IM的方式试图做到这一点:
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);
}
}
}
据我得到,当你检查 DataGridViewCheckBoxCell
,单元格的值是真正
吧?但它不是让我值转换为bool,然后比较一下,扔我一个无效的转换异常。
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.
推荐答案
尝试
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)
{
}
这篇关于从DataGridViewCheckBoxCell获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!