如何验证 DataGridViewCheckBoxCell 是否已选中
我已将数据表绑定到DataGridView
,该数据表有一个名为“Status”的列,其类型为Boolean
。我可以通过代码将值设置为 true
或 false
。
但是,我不知道如何检查给定的行是否已被检查。这是我尝试使用的代码,编译它显示错误“指定的强制转换无效”。
任何帮助将不胜感激。
if (rowIndex >= 0)
{
var cbxCell = (DataGridViewCheckBoxCell)dgvScan.Rows[rowIndex].Cells["Status"];
if ((bool)cbxCell.Value)
{
// Do stuff
}
else
{
// Do other stuff
}
}
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.
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
谢谢大家。
有同样的问题,但我发现在检查值之前编写 senderGrid.EndEdit() 可以解决它。
继续做好工作
Thank you all.
Had the same problem but i find out that writing senderGrid.EndEdit(), before checking the value, resolves it.
Keep up the good work
问题在于 DataGridCheckBoxColumn 的默认 FALSE 值为 null,而默认 TRUE 值为布尔值 True。这会导致问题,因为布尔值不可为空。您可以通过两种方式解决此问题:
解决此问题的另一种方法是将列的 TrueValue 属性设置为某个值。这可以在设计时完成,如下所示:
然后您可以编写:
这有效,因为字符串可为空。
请注意:即使我将 FalseValue 设置为 F,假值似乎仍然为 null,因此我建议忽略 FalseValue 属性。
另请注意:如果您按上述方式在 TrueValue 中放入某些内容,然后尝试删除它,则 True value 会变为 null(哎呀),需要您删除该列,然后重新添加它才能将其恢复为默认状态。或者您可以在代码中更改它,如下所示:
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:
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:
Then you can write:
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.
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:
可能遇到的另一个问题是:
当用户单击单元格来选中或取消选中该框时,基础值不会更改,直到单元格失去焦点。
如果相关代码位于按钮中,这不会成为问题,因为单击按钮时单元格将失去焦点。但是,如果您的代码是从计时器触发的,您可能仍在检查“旧”值。
在这里查看我的另一个答案:
https://stackoverflow.com/a/22080846/1015072
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
我以前没有这方面的经验,但我想您应该检查列或属性的值。
尝试看一下这个示例:
http:// /programmingwithstyle.blogspot.com/2007/06/how-to-get-from-datagridviewcheckboxcel.html
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
CbxCell.Value
必须等于DBNull.Value
(您的列可以包含空值,对吗?)我会在转换之前检查 DBNull:
CbxCell.Value
must be equal toDBNull.Value
(your column can contain null values right?)I would check for DBNull before casting: