[英]Datagridview to array missing one row
我有一个带有CheckBox
第一列的DataGridView
。
我使用以下Linq来获取所有选中的行。
DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>(!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value))).ToArray();
但是不知何故结果总是缺少最后检查的行!!!
但是,如果我选择另一卷纸(不检查它),则在运行该行之前,将显示最后一行!!!
有人可以这么好心告诉我我在哪里做错了!?
非常感激!!!
您正在使用以下条件:
!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value)
使用&&
此条件必须在左右两个方向都成功。
现在我的问题是:
Convert.ToBoolean(x.Cells[0].Value)
=> not a boolean? where clause return as false.
=> what is the purpose? this code doesn't have a reason anymore. You are just
converting it to boolean
我建议您只能尝试以下方法:
DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>(!Convert.IsDBNull(x.Cells[0].Value)))
原来,当我运行代码时, DataGridView
仍处于“ 编辑模式 ”,这意味着检查不是“最终的” !
这就是Linq找不到它的原因!
所以我加了
dgvMain.EndEdit();
在Linq查询和问题解决之前!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.