Unable to refresh/update data grid after insert in Silverlight 4
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 d开发者_高级运维atabase 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.
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.
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication1"
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.DataContext>
<local:MainPage_ViewModel/>
</UserControl.DataContext>
<StackPanel>
<data:DataGrid ItemsSource="{Binding MyCollection}">
</data:DataGrid>
</StackPanel>
public class MainPage_ViewModel : INotifyPropertyChanged
{
public ObservableCollection<object> MyCollection
{
get { return myCollection; }
set
{
if (myCollection != value)
{
myCollection = value;
OnPropertyChanged("MyCollection");
}
}
}
private ObservableCollection<object> myCollection = new ObservableCollection<object>();
public void LoadData()
{
//execute load method, the assign result to MyCollection
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
In my ViewModel I initially create a new DomainContext():
public DocumentWindowViewModel()
{
_Context = new appDomainContext();
WireCommands();
}
I ended up having to copy the following line to my query:
_Context = new appDomainContext();
And paste to the function that grabs the values from the database:
public void GetDocuments(int dID)
{
_Context = new appDomainContext(); //ADDED THIS LINE TO FIX MY ISSUE
_Context.Load(_Context.GetDocumentsQuery(dID), dlo =>
{
DocumentsView = dlo.Entities;
}, null);
}
}
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();
精彩评论