0

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.

marczellm
  • 1,224
  • 2
  • 18
  • 42
  • "My model classes are automatically generated from the database by Entity Framework, so they cannot implement INotifyPropertyChanged." This is not true, you can modify the T4 template that generates your classes and have them implement INPC. Not saying this is the solution just saying that was a false statement. http://blogs.msdn.com/b/efdesign/archive/2009/01/22/customizing-entity-classes-with-t4.aspx – Lee O. Jul 23 '14 at 14:08

3 Answers3

0

You will have to write ViewModel wrappers that implement INotifyPropertyChanged as describe in this post.

Community
  • 1
  • 1
Nitin Joshi
  • 1,638
  • 1
  • 14
  • 17
  • Okay, but I have over 70 tables with nearly 40 attributes in some cases. Is there no way to simply reload the DataGrid contents from the database? – marczellm Jul 23 '14 at 14:12
  • Reloading the DataGrid will be an expensive operation if you have huge set of records. e.g. if you have a table with 1 Cr records and if you made changes in a single field in a record, it will be updated in the db and then if you want to reload the DataGrid for this small change you will have to probably fetch all records and bind the record set with the DataGrid again. This operation will take huge time and user will have to wait unnecessarily. – Nitin Joshi Jul 23 '14 at 14:27
0

You could cause the collection to get 'rebound' everytime. Though, as mentioned by Nitin Joshi, it may be expensive.

Something like:

xaml:

<DataGrid ItemsSource={Binding CollectionView, UpdateSourceTrigger="PropertyChanged"}/>

code:

private DbSet _data;

ICollectionView _collectionView;
public ICollectionView CollectionView 
{
    get { return _collectionView; }
    set
    {
        _collectionView = value;
        RaisePropertyChanged("CollectionView");
    }   
}

and then instead of calling Refresh(), you can simply 're-bind' the CollectionView with the new data set.

private void Update()
{
    _data.Load();
    CollectionView = null; // this will force a re-bind
    CollectionView = CollectionViewSource.GetDefaultView(_data.Local);  
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • This did not work for me; the DataGrid still displays the old data even after setting `CollectionView` to null and setting it back and raising `PropertyChanged` both times. – marczellm Jul 24 '14 at 09:15
  • I take it back; turns out the problem was 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. – marczellm Jul 24 '14 at 12:40
0

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.

marczellm
  • 1,224
  • 2
  • 18
  • 42