2

When I remove an item from an ObservableCollection that has invalid data in it the datagrid won't clear the fact that it has errors so once I delete it it acts like the DataGrid still has errors and won't allow me to edit/add and edit data.

I'm using MVVM so I can't just do datagrid.refresh :\

Any ideas?

3
  • Is the item getting deleted from the ViewModel and your not seeing the change in the DataGrid? Are you using INotifyPropertyChanged? Try adding UpdateSourceTrigger="PropertyChanged" to the source binding for the items of the DataGrid.
    – d.moncada
    Feb 21, 2012 at 17:31
  • If I'm not mistaken, when an exception occurs the binding is broken. Feb 21, 2012 at 17:48
  • its getting delete from the viewmodel and it updates in the datagrid, but the datagrid does not know that there is no longer any invalid rows Feb 22, 2012 at 18:37

3 Answers 3

2

I don't know if this will work, but you can try telling the data grid the entire collection has changed:

Two options:

1) Raise a property change notification for the collection property.

public class MyViewModel : ViewModelBase
{
    private void RefreshItems()
    {
        RaisePropertyChanged("Items");
    }

    private ObservableCollection<DataItem> Items { ... }
}

2) Derive from ObservableCollection so that you can raise a NotifyCollectionChanged event

public class MyCollection : ObservableCollection<DataItem>
{
    public void Refresh()
    {
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}
1
  • This kind of works, it will allow me to edit new rows but not rows that were created before the deletion of the invalid row. Feb 22, 2012 at 19:07
1

I came up with this using Phil's answer:

    protected override void RemoveItem(int index)
    {
        this[index] = new EngineStatusUserFilter();
        base.RemoveItem(index);
        Refresh();

    }

    public void Refresh() {
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } 

I set the old object to a new one before removing it so it will be valid.

0
0

After removing item (that have validation error) from ObservableCollection, recreate ObservableCollection and raise OnPropertyChanged.

This refresh DataGrid, rows you are created before delete remains and are editable because validation error of removed item/row is gone.

Like this:

public ObservableCollection<Person> Persons { get; private set; }
...
private void DeleteRowCommand_Method()
{
    Persons.Remove(SelectedPerson);
    Persons = new ObservableCollection<Person>(Persons);
    OnPropertyChanged("Persons");
}
...
1
  • This seems to resolve the issue, but unlike the solutions above that broadcast a "Reset," this has a side-effect of undoing any sorting that was applied to the grid columns. Dec 5, 2014 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.