3

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?

1
  • 1
    Can you show us the XAML for the binding and the code for the data context object? Commented May 25, 2011 at 16:15

4 Answers 4

2

You are not seeing the updates as the ObservableCollection<T> is obtaining those values from the entity set once, during construction. All additional changes to the entity set are not "observed".

Only calls to ObservableCollection<T>.Add (et al) generate the CollectionChanged events.

Sign up to request clarification or add additional context in comments.

Comments

1

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

0

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

I am binding to the ObservableCollection(context.EntitySet). My new entity is PROPERLY in the context, but DataGrid is not updated. I am using MVVM pattern.
0

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.

1 Comment

Sounds good, but doesn't work. I added entity to the context, nothing did with ObservableCollection above it, then called PropertyChanged("MyObservableCollection") and nothing happens. Please, do you have any other idea???

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.