DataGrid with ADO.NET Entity Framework and MVVM Best Practice

Jun 3, 2009 at 9:51 AM

What is the best practice to produce a ViewModel for a WPF DataGrid with ADO.NET Entity Framework data-binding? More in detail:

I have the Project entity (which is a Visual Studio entity designer generated (ADO.NET entity) class actually, with INotifyPropertyChanged etc.)

I created a ProjectEntityViewModel wrapping the Project entity type and mostly exposing the Project entity's properties. I also have a DataGridViewModel which is related to a view containing a WPF DataGrid. I want to be able to query the ObjectContext and then populate some ProjectViewModel and then bind another ViewModel which contains an ObservableCollection of ProjectViewModels to a WPF DataGrid for adding/removing/editing items.

Now when I create an ObservableCollection<ProjectentityViewModel> within the DataGridViewModel and populate it using the ADO.NET ObjectContext's ObjectQuery to List() (that is I get the List<Project> projects = myObjectContext.Projects.ToList() and then iterate this list to populate ObservableCollection<ProjectEntityViewModel> of DataGridViewModel). In this case when I add a new item using the DataGrid (that is CanUserAddRows=True for DataGrid), the ObservableCollection<ProjectEntityViewModel> gets updated since it's bound to DataGrid in question but ObjectContext can not see the added item since the ObservableCollection<ProjectEntityViewModel> and the Project list of the ObjectContext (List<Project> projects) are seperate. What is the best practice to achieve this? Should I handle the CollectionChanged event of the ObservableCollection<ProjectEntityViewModel> and then Add/Remove the items to/from the ObservableCollection to/from the ObjectContext manually or is there a better way to do this? This looks like too much hassle (that is creating a ViewModel for entities then creating ObservableCollections for those entity viewmodels within container ViewModels and keeping those Observable collections in sync with ObjectContext manuallly just to avoid some WPF ValueConverters and having the ablility to generate custom presentation rules etc.)

So can you please tell me the best way to update the ObjectContext when a ViewModel's ObservableCollection (bound to a DataGrid) of entity ViewModels is changed by adding/removing items?

Note that I don't have and don't want to have any Buttons on the Datagrid rows, so binding commands to those and performing Add/Remove is out of option. I want to preserve the automatic behavior of adding a new row and deleting a row with Del key of the DataGrid.

I'd have shown some code but unfortunately the software I am working on is a commercial one so I can't. I hope I was able to explain the scenario well enough.

Thanks in advance for the answers. I really liked the idea of MVVM but it's still confusing at some points for me so I am interested in learning/understanding the best practices.



Jul 16, 2009 at 8:22 PM


I'm also looking for more information about using CodePlex DataGrid with Entity Framework. Don't know if the best aproach is to bind directly or to use an intermediate observeable collection class. Either whay, don't know how to perform sucessfully 2-way binding.

If you have any updated information, please let me know.



Jun 7, 2010 at 12:14 PM
Edited Jun 7, 2010 at 12:17 PM
I would also like to know how this could be done. Any help would be much appreciated. I'm using EF 4, MVVM and I would like to have a solution that links my view to a collection in a viewmodel with some code to load the data into that collection. Thanks, Richard