简体   繁体   English

Datagridview数组缺少一行

[英]Datagridview to array missing one row

I have a DataGridView with a CheckBox first column. 我有一个带有CheckBox第一列的DataGridView

I use the following Linq to get all the checked rows. 我使用以下Linq来获取所有选中的行。

DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>(!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value))).ToArray();

But somehow the result ALWAYS missing the last checked row!!! 但是不知何故结果总是缺少最后检查的行!!!

BUT, if I select another roll (not checking it), before I run the line, the last row showed up!!! 但是,如果我选择另一卷纸(不检查它),则在运行该行之前,将显示最后一行!!!

Could somebody please be so kind and tell me where did I do wrong!? 有人可以这么好心告诉我我在哪里做错了!?

Much appreciated!!! 非常感激!!!

You are using this condition: 您正在使用以下条件:

!Convert.IsDBNull(x.Cells[0].Value) && Convert.ToBoolean(x.Cells[0].Value)

Using && this condition must succeed both left and right. 使用&&此条件必须在左右两个方向都成功。

now my Question is: 现在我的问题是:

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

I suggest you can try only this: 我建议您只能尝试以下方法:

DataGridViewRow[] drs = dgvMain.Rows.Cast<DataGridViewRow>().Where(x =>(!Convert.IsDBNull(x.Cells[0].Value)))

It turned out that the DataGridView is still in Edit Mode when I ran the code, which means the check is not "final" ! 原来,当我运行代码时, DataGridView仍处于“ 编辑模式 ”,这意味着检查不是“最终的”

That's why the Linq was unable to find it! 这就是Linq找不到它的原因!

So I added 所以我加了

dgvMain.EndEdit();

before the Linq query and the problem is solved!!! 在Linq查询和问题解决之前!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM