locked
The correct way to refresh the bound datagrid? RRS feed

  • Question

  • Hi I have a simple DataGrid that is bound to DomainDataSource in code behind I add a new entity to associated DataContext and SubmitChanges. I see that the new record goes into database, that's good, however the DataGrid doesn't show the newly created entity. After googling a bit I found this post http://silverlight.net/forums/p/111826/254748.aspx I followed the links from there and changed the logic of the program as such 1. Add entity to datacontext 2. DataContext.SubmitChanges 3. in callback for SubmitChanges if HasError is false I add the new entity to DomainDataSource.DataView. This works fine, I have record in DB and in DataGrid. However when trying to sort the DataGrid the newly created entity is shown twice. (There is only one record in DB!) Any idea what am I doing wrong? Or what's the correct way to refresh the bound DataGrid? Thx
    Wednesday, August 12, 2009 1:23 AM

Answers

  • well, after waiting here and googling I finally managed to find a solution that works more or less as expected. And the solution is pretty simple actually, after you do DOmainContext.SubmitChanges() and check that there are no error, you have to call DomainDataSOurce.Load() as well, this will make the associated DataGrid refresh properly. If anyone knows a better solution please post here.
    Thursday, August 13, 2009 3:43 AM

All replies

  • Hi Wow, nobody has this problem. How do you guys add a enw record and its displayed immediate in datagrid? By loading again form server? Imho, not a good solution Any ideas, welcome!
    Thursday, August 13, 2009 1:55 AM
  • well, after waiting here and googling I finally managed to find a solution that works more or less as expected. And the solution is pretty simple actually, after you do DOmainContext.SubmitChanges() and check that there are no error, you have to call DomainDataSOurce.Load() as well, this will make the associated DataGrid refresh properly. If anyone knows a better solution please post here.
    Thursday, August 13, 2009 3:43 AM
  • I have that problem.

    It's good that the solution works for you. But in my case it's different.

    I update the data from a childwindow via a ServiceOperation function in the domain service. After the data is saved in the db, the childwindow closes. I do call:

    MyDomainDataSource.Load();

    however, my datagrid, does not refresh itself. I google and found that people are mentioning about Dispatcher. Well, i've tried:

    mydatagrid.dispatcher.begininvoke(() => mydomaindatasource.load());

    again, nothing happen. The MyDomainDataSource.Load still call the function from the Domain Service, but the UI remains the same.

    Friday, August 14, 2009 6:22 PM
  •  

    @wishstar99

    You may have solved this already but if not, then once you confirm that the data has been correctly loaded into the database try setting the itemssource of the datagrid to null before setting it back.

    MyDomainContext myDomainContext = (MyDomainContext)mydomaindatasource.DomainContext;
    
    mydatagrid.ItemsSource = null;
    mydatagrid.ItemsSource = myDomainContext.myCollection;
    
    
      

    ..where MyDomainContext is your Context and myCollection is the data the datagrid is normally bound to.

    Delordson

    Wednesday, August 19, 2009 1:42 AM
  • Delordson,

    thank you for the reply. Well, setting mydatagrid.itemssource = null will clear the datagrid.
    however, i'm using a riacontrol:domaindatasource x:name="mydatasource"

    and the <data:mydatagrid binding="{Binding Data, ElementName=mydatasource}"


    so if do:

    mydatagrid.itemsSource = null;
    mydatagrid.binding ...??? What should i put?

    here's the thread that i posted, but still no luck: http://silverlight.net/forums/t/119570.aspx

    thank you again.

    Thursday, August 20, 2009 7:56 PM
  • I update the data from a childwindow via a ServiceOperation function in the domain service.

    The ServiceOperation won't merge the entities on return. The CustomOperation and SubmitChanges will merge the result. So in your scenario with the ServiceOperation you send a clone of the entities to the server, update the database, but the local entities in the domain context are not touched.

    Now the problem with the CustomOperation is the fact you have to call SubmitChanges before it actually triggers the async request. And there is a bug that you can't have broken validation contraints when calling the custom operation.

    Friday, August 21, 2009 3:26 AM