![](/img/trans.png)
[英]Accessing items in ObservableCollection bound to WPF DataGrid
[英]Removing items from a observablecollection bound to a datagrid Silverlight?
從可綁定到datagrid中的datagrid的observablecollection中刪除行時出現錯誤。
System.NullReferenceException:對象引用未設置為System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)處的對象實例。
if (GV.orderItemList.Contains(oOrdritem))
GV.orderItemList.Remove(oOrdritem);
第一次,我刪除一條記錄,它的工作正常,第二次,它給出了以上異常
請請幫助
嘗試使用RemoveAt而不是Remove,在某些情況下它對我有用。 首先,您將找到de item的索引:
int index = collection.IndexOf(item);
然后您嘗試刪除:
collection.RemoveAt(index);
做一個空檢查:(或幾個,不確定代碼的其余部分是什么樣,所以我要強制執行所有操作)
if (GV != null && GV.orderItemList != null && oOrdritem != null && GV.orderItemList.Contains(oOrdritem))
GV.orderItemList.Remove(oOrdritem);
我使用以下代碼對問題進行了排序:
dgOrderItems.CommitEdit(DataGridEditingUnit.Row, true);
當前焦點所在的行仍處於編輯模式,這導致了異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.