C# 如何验证是否选中了 DataGridViewCheckBoxCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1563190/
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
How to verify if a DataGridViewCheckBoxCell is Checked
提问by
I have bound a data table to a DataGridView
, this data table has a column called "Status" which is of type Boolean
. I can set the value to true
or false
just fine via code.
我已将数据表绑定到DataGridView
,该数据表有一个名为“状态”的列,其类型为Boolean
。我可以通过代码将值设置为true
或false
就好了。
However, I can't figure out how to check to see if the given row is already checked or not. This is the code I am trying to use and compiling it shows the error "the specified cast is invalid".
但是,我不知道如何检查给定的行是否已被检查。这是我尝试使用的代码,编译它显示错误“指定的演员表无效”。
Any help would be appreciated.
任何帮助,将不胜感激。
if (rowIndex >= 0)
{
var cbxCell = (DataGridViewCheckBoxCell)dgvScan.Rows[rowIndex].Cells["Status"];
if ((bool)cbxCell.Value)
{
// Do stuff
}
else
{
// Do other stuff
}
}
回答by Roberto Aloi
I have no prior experience in this, but I guess you should check the value of the column or property.
我之前没有这方面的经验,但我想您应该检查列或属性的值。
Try to have a look to this example:
试着看看这个例子:
http://programmingwithstyle.blogspot.com/2007/06/how-to-get-from-datagridviewcheckboxcel.html
http://programmingwithstyle.blogspot.com/2007/06/how-to-get-from-datagridviewcheckboxcel.html
回答by manji
CbxCell.Value
must be equal to DBNull.Value
(your column can contain null values right?)
CbxCell.Value
必须等于DBNull.Value
(您的列可以包含空值,对吗?)
I would check for DBNull before casting:
我会在投射之前检查 DBNull :
if (!DBNull.Value.Equals(CbxCell.Value) && (bool)CbxCell.Value == true)
{
//Do stuff
}
else
{
//Do Stuff
}
回答by sk2185
if (Convert.ToBoolean(dgvScan.Rows[rowIndex].Cells["Status"].Value))
{
//Do Something
}
else {
// Do Something
}
回答by Mark Ainsworth
The problem is that the default FALSE value for a DataGridCheckBoxColumn is null, and the Default TRUE value is the boolean value True. This causes a problem because boolean values are not nullable. You can solve this problem two ways:
问题是 DataGridCheckBoxColumn 的默认 FALSE 值为空,而默认 TRUE 值是布尔值 True。这会导致问题,因为布尔值不可为空。您可以通过两种方式解决此问题:
if (cbxCell.Value != null && (bool)cbxCell.Value)
{
do stuff;
}
The other way to solve this is set the TrueValue property of the column to some value. This can be done at design time as shown:
解决此问题的另一种方法是将列的 TrueValue 属性设置为某个值。这可以在设计时完成,如下所示:
Then you can write:
然后你可以写:
if ((string)cbxCell.Value == "T")
{
do stuff;
}
This works because Strings are nullable.
这是有效的,因为字符串可以为空。
Please note: Even though I set the FalseValue to be F the false value still seems to be null, so I suggest ignoring the FalseValue property.
请注意:即使我将 FalseValue 设置为 F,假值似乎仍然为空,因此我建议忽略 FalseValue 属性。
One other note: IF you put something in TrueValue as above and then attemp to erase it, True value becomes null (ouch), requireing you to delete the column and then re-add it in order to restore it to the default condition. Or you can change it in code as follows:
另一个注意事项:如果您像上面那样在 TrueValue 中放入某些内容然后尝试将其擦除,则 True 值变为空(哎哟),需要您删除该列然后重新添加它以将其恢复为默认条件。或者您可以在代码中更改它,如下所示:
((DataGridViewCheckBoxColumn)DataGridView1.Columns["Selected"]).TrueValue = true
回答by Allen
Another issue that may be encountered is this:
另一个可能遇到的问题是:
When the user clicks the cell to check or uncheck the box, the underlying value will not be changed until the cell loses focus.
当用户单击单元格以选中或取消选中该框时,在单元格失去焦点之前不会更改基础值。
This will not be an issue if the code in question is in a button, since the cell will lose focus when you click the button. But if your code is fired from a timer, you may still be checking the 'old' value.
如果有问题的代码在按钮中,这将不是问题,因为单击按钮时单元格将失去焦点。但是,如果您的代码是从计时器触发的,您可能仍在检查“旧”值。
See my other answer here: https://stackoverflow.com/a/22080846/1015072
在此处查看我的其他答案:https: //stackoverflow.com/a/22080846/1015072
回答by John
bool checked = cell.Value as bool? ?? false;
回答by Lafuente
Thank you all. Had the same problem but i find out that writing senderGrid.EndEdit(), before checking the value, resolves it.
谢谢你们。有同样的问题,但我发现在检查值之前编写 senderGrid.EndEdit() 可以解决它。
private void dgvRiscos_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
senderGrid.EndEdit();
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
e.RowIndex >= 0)
{
var cbxCell = (DataGridViewCheckBoxCell)senderGrid.Rows[e.RowIndex].Cells["associado"];
if ((bool)cbxCell.Value)
{
// Criar registo na base de dados
}
else
{
// Remover registo da base de dados
}
}
}
Keep up the good work
保持良好的工作