Silverlight 4 with RIA Services - Refresh Datagrid bound to CollectionView
Here is my situation:
I have a domain datasource in my Silverl开发者_如何学JAVAight 4 page. I've pulled associated entities from RIA Services and displayed them on the same page using a collectionview.
In a button click event I insert/add an entity (see code snippet below).
How do I get the datagrid to refresh? What am I doing wrong?
I know the data is being inserted into the database just can't get the grid to refresh without leaving the page and coming back.
DomainContext ctx = new DomainContext();
foreach (<Entity> x in EntityList)
{
<Entity> y = new <Entity>
{
.... <set values>
};
ctx.<Entity>.Add(y);
}
ctx.SubmitChanges();
DomainDataSource.Load(); ;
CollectionView.View.Refresh();
Try changing your load to use RefreshCurrent.
ctx.Load( query, LoadBehavior.RefreshCurrent, GetCategoriesByLevelQuery_Loaded, null );
The three load behaviors available:
Keep Current (default): means that the version that is cached on the client is not changed with the load operation. Entities will not be updated with new information.
Merge Into Current: if there has been no modification to a cached entity, it will be updated with the load operation entities. This seems to be the safest option if the user will be editing data because the user will not want to lose data they have been entering before a submit.
Refresh Current: all entities in the cache will be updated with information from the load operation entities. This has the possibility to overwrite a change that the user has made but not committed. Be careful with this option.
My problem was adding "outside" my domaindatasource.
Here is what I ended up doing:
DomainContext ctx = (MyDDS)DomainDataSource.DomainContext; //new DomainContext();
foreach (<Entity> x in EntityList)
{
<Entity> y = new <Entity>
{
.... <set values>
};
ctx.<Entity>.Add(y);
}
ctx.SubmitChanges();
....
private void MyDDS_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
{
MyDDS.Load();
}
Not sure if this is the optimal way to do it but it worked for me.
The correct way will be to add the new item in the DomainDataSource.DataView and not in the DomainDataSource.DataContext:
In your original code:
DomainDataSource.DataView.Add(y);
This will have some implications with paging. The new items will be added in the current page, extending its size. After a refresh, the size will be adjusted.
Reference: here
精彩评论