I am creating a master detials silverlight page and am having trouble updating the datagrid after inserting a new record. On the top of the page I have text boxes to enter information, when a user clicks on save I would like the information automatically updated in the database and displayed on the datagrid without refreshing the screen.
I am able to save the information to the db with no problems its just trying to get the datagrid to refresh with the new changes.
Some of my code behind for the SAVE button is below:
ViewModel.UpdateWorkflow(summary, reason, Email);
LoadOperation<Document> lo = _Context.Load<Document>(_Context.GetDocumentsQuery(_DocID), rtRefresh, null);
The code for rtRefresh:
private void rtRefresh(LoadOperation<Document> oLoadOperation)
{
if (oLoadOperation.IsComplete)
{
ViewModel.GetDocuments(_DocID);
}
}
I set the ViewModel in the xaml file as:
<controls:ChildWindow.Resources>
<viewModel:DocumentChildWindowViewModel x:Key="ViewModel" />
</controls:ChildWindow.Resources>
And the ViewModel in the code-behind:
ViewModel = Resources["ViewModel"] as DocumentChildWindowViewModel;
Any help would be appreciated, thank you.
In my ViewModel I initially create a new DomainContext():
I ended up having to copy the following line to my query:
And paste to the function that grabs the values from the database:
the _Context was keeping old values. This cleared the _Context and allowed me to have the updated data in the _Context.
Your problem could be related to the default LoadBehavior for the domain context Load() method. Try setting the LoadBehaviour explicitly to LoadBehaviour.RefreshCurrent. For more on LoadBehaviour please see: http://msdn.microsoft.com/en-us/library/system.servicemodel.domainservices.client.loadbehavior(v=VS.91).aspx.
Sometimes, for example after a delete, you will need to also Clear() the EntityContainer on the domain context or atleast Clear() the particular EntitySet<> for the entity you are trying to load, for the right entities to be displayed.
like, MyDomainContext.EntityContainer.GetEntitySet().Clear();
I'm assuming your datagrid is bound to an IEnumerable, maybe a List<>? Have you tried using an ObservableCollection<>? If you are bound to an ObservableCollection, it will let the UI know when the collection has changed.