簡體   English   中英

Datagridview數組缺少一行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM