C# 从 DataGridViewCheckBoxCell 获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13632536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value from DataGridViewCheckBoxCell
提问by Bryan Arbelo - MaG3Stican
I am working on a DataGridViewcalled ListingGridtrying to activate / deactivate users that have been "checked" on any DataGridViewCheckBoxCellthat is inside the DataGridViewCheckBoxColumn.
我工作的一个DataGridView叫ListingGrid试图对任何已被“选中”激活/停用用户DataGridViewCheckBoxCell是内部的DataGridViewCheckBoxColumn。
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);
}
}
}
As far as I get, when you check a DataGridViewCheckBoxCell, the value of the cell is trueright? But it is not allowing me to convert the value to bool and then compare it, throwing me an invalid cast exception.
据我所知,当您检查 a 时DataGridViewCheckBoxCell,单元格的值是否true正确?但是它不允许我将值转换为 bool 然后比较它,从而抛出一个无效的强制转换异常。
采纳答案by Developer
try:
尝试:
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chkchecking.Value) == true)
{
}
or
或者
DataGridViewCheckBoxCell chkchecking = roow.Cells[0] as DataGridViewCheckBoxCell;
if ((bool)chkchecking.Value == true)
{
}
回答by Pruno
回答by Nabil
I do it usually like this
bool value = (short)chkchecking.Value == 1
我通常这样做
bool value = (short)chkchecking.Value == 1

