Search code examples
c#wpfentity-frameworkdatagridwpfdatagrid

Refresh DataGrid after item property change


My model classes are automatically generated from the database by Entity Framework, so they cannot implement INotifyPropertyChanged. I have this in my XAML:

<DataGrid ItemsSource={Binding CollectionView}/>

and in the class that acts as DataContext:

public ICollectionView CollectionView { get; set; }
private DbSet _data;

and in its constructor:

_data.Load();
CollectionView = CollectionViewSource.GetDefaultView(_data.Local);

Another part of the application makes a database update, which changes one property of an entity instance. After this I call

_data.Load();
CollectionView.Refresh();

but the update does not appear in the DataGrid. What should I do?

What's weird to me is that I could make a database insert appear with _data.Load() so I got the impression that this call completely rebuilds the Local collection from the database.


Solution

  • It turns out the problem was that the view that modified the database and the view that displayed the data were using two different instances of the same DbContext class; wrapping it with a singleton fixed the problem.