I am using Entity Framework 4/WPF/C#. The new ObservableCollection<Entity>(context.EntitySet) is bound to the WPF DataGrid in XAML. This works fine. But when I programatically create a new Entity and add it to the context, DataGrid remains unchanged. What should I do, to get DataGrid to update?
-
1Can you show us the XAML for the binding and the code for the data context object?user7116– user71162011-05-25 16:15:23 +00:00Commented May 25, 2011 at 16:15
4 Answers
As sixlettervariables said, the constructor you are using takes your context.EntitySet values and adds them all to the new ObservableCollection. Adding do your context.EntitySet does not add to the duplicated list of items.
Can you show how your EntitySet is defined? Would it be possible to make it an ObservableCollection and bind directly to it?
Comments
I didn't see silverlight mentioned so i'm guessing you are not using RIA services. How are you adding the object to the context? .AddTo{EntitySetName]() or .{EntitySetNme}.Add()
If you are binding directly to the data context, I believe the latter option is your best bet. Personally I would shy away from this, as your presentation layer is talking directly to your data layer. You should establish a go between (business layer) that handles adding entities to the UI bound collection and persistence separatly.
1 Comment
well its all right ;) how should the datagrid know that you add an item to your context? the datagrid would just be informed if you add an item to your observable collection.
so if you add the item do your context, just call OnPropertyChanged("YourEntityCollectionPropertyHere") for your collection property.