sony
sony

Reputation: 1503

Removing items from a observablecollection bound to a datagrid Silverlight?

I am getting an error when deleting rows from an observablecollection which is bound to a datagrid in silverlight.

System.NullReferenceException: Object reference not set to an instance of an object.at at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)

if (GV.orderItemList.Contains(oOrdritem))
    GV.orderItemList.Remove(oOrdritem);

The first time, I delete a record, its working fine, the second time, it gives the above exception

enter image description here

please please please help

Upvotes: 0

Views: 3955

Answers (3)

sony
sony

Reputation: 1503

I sorted the problem using the code below:

dgOrderItems.CommitEdit(DataGridEditingUnit.Row, true);

The row which was currently focused was still in edit mode which was causing the exception.

Upvotes: 1

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

Do a null check: (Or several, not sure what the rest of your code looks like so I'm enforcing everything)

if (GV != null && GV.orderItemList != null && oOrdritem != null && GV.orderItemList.Contains(oOrdritem))
    GV.orderItemList.Remove(oOrdritem);

Upvotes: 1

Vinicius
Vinicius

Reputation: 541

Try to use RemoveAt instead of Remove, it worked for me in some cases. First you find the index of de item with :

int index = collection.IndexOf(item);

then you try to remove :

collection.RemoveAt(index);

Upvotes: 1

Related Questions