none
Refresh Datagrid/dataPager after record added - silverlight 3, WCF ria services Nov 09 release

    Question

  • Hi guys,

    I have a child window to add a new record which works fine. BUT when i've added a new record and the child window closes, the datagrid & datapager on my main form don't show the added records until i click on the pager to move to next/previous page.

    I would liek to be able to show the added record automatically after it is added. So is there a way of refreshing the datagrid & the datapager to show this?

    I've tried the .Load() but this seems to be a query load for the domain context & doesn't exist for the datagrid itself.

    Also, i tried to re-bind the datagrid's itemsource after the record is added but when you exceed the pager pagesize, instead of creating another page, it adds the record to the current page and displays scrollbars in the datagrid (until you refresh the whole page manually and then it corrects itself). The code i used for this is as follows:

    void NewSusLog_Closed(object sender, EventArgs e)

    {

    try

    {

    AddNewRec newsusrec = (AddNewRec)sender;if (newsusrec.DialogResult == true )

    {

    SusLogDomainContext ctx = (SusLogDomainContext)MyData.DomainContext;

    ctx.SUS_ExportLogs.Add(newsusrec.newsuslog);

    ctx.SubmitChanges();

    this.SusExportLogGrid.ItemsSource = (IEnumerable)ctx.SUS_ExportLogs;

    //this.SusPager.Source = (IEnumerable)ctx.SUS_ExportLogs;

    //this.SusPager.PageSize = 5;

     

    }

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    }

    So if you have an alternative method that actually works in refreshing the datagrid then please could you let me know?

    Regards, Shuja.

    Tuesday, December 01, 2009 5:50 AM

Answers

  • It looks like you are adding the new entity into the DomainContext rather than into the DomainDataSource's DataView. DomainDataSource will only surface entities that were either loaded through it or added through it, so when you add the entity to the underlying DomainContext, it won't show up (as you're seeing). Check out DomainDataSource.DataView.Add; when you add to the DomainDataSource through that method, the entity will be reflected immediately. This page talks more about the DataView property and its API: http://jeffhandley.com/archive/2009/11/16/domaindatasourceview-again.aspx When you add an item through the DomainDataSource, and you have paging enabled, the added item will always be added to the current page. Because we cannot perform additional loads once you have pending changes (see http://jeffhandley.com/archive/2009/07/14/domaindatasourcesurvey.aspx for background), we don't want to let you push entities off the current page onto other pages when you have changes. So as items are added, the size of the current page will increase, and conversely, when you remove items, the size of the current page will decrease. Once you submit changes and reload the data, pages will return to their configured size.
    Tuesday, December 08, 2009 1:19 PM

All replies

  • I believe that if you are using an ObservableCollection then it should update automatically.  But, I'm not and I have to force a reload of the DomainDataSource and change the MergeOptions to OverwriteCurrentValues.

    Tuesday, December 01, 2009 7:19 AM
  • Hi Ardman,

    What has happened is that when i had the July 09 version of ria services installed, i was able to update the datagrid after a record was added by simply reloading the domain data source & the rebinding it through the datagrid itemsource. This seemed to work fine.

    However, i have since updated the ria services to the Nov 09 version and now when i use this method to update the datagrid, the record appears in the datagrid but the list of items & the datapager gets distorted (e.g. if i have a datapager of 5 then when the record is added the list of records on one page exceeds 5 and gives me a horizontal & vertical scrollbar. So the record is added by the paging seems to get confused until i manually refresh the page).

    What i would like to know is: is this a known bug within the ria services nov 09 update? If so, is there a work around to update/refresh the datagrid after a record is added from a child window?

    Ardman: where would i need to change the merge options?

    Regards, Shuja

    Tuesday, December 08, 2009 7:56 AM
  • I think part of the problem is that your grid refresh code was there to work around bugs in the DDS. Those bugs are may now be fixed in the DDS so now the old workaround is causing the problem. I don't know for sure, but that is what I would try.

    Tuesday, December 08, 2009 8:13 AM
  • Hi ColinBlair,

    The code i'm using to reload the DDS & refresh the data within the datagrid is as follows:

    void NewSusLog_Closed(object sender, EventArgs e)

    {

    try

    {

    AddNewRec newsusrec = (AddNewRec)sender;if (newsusrec.DialogResult == true )

    {

    SusLogDomainContext ctx = (SusLogDomainContext)MyData.DomainContext;

    ctx.SUS_ExportLogs.Add(newsusrec.newsuslog);

    ctx.SubmitChanges();

    ctx.Load(ctx.GetSUS_ExportLogQuery());

    this.SusExportLogGrid.ItemsSource = (IEnumerable)ctx.SUS_ExportLogs;

    //this.SusPager.Source = (IEnumerable)ctx.SUS_ExportLogs;

    //this.SusPager.PageSize = 5;

     

    }

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    }

     

    This captures the child window close event and then adds the new record. The code to refresh the datagrid is highlighted in bold. This worked in July 09 ria services but doesn't work correctly in nov 09 ria services (as mentioned in my previous post).

    If i disable this code, it simply goes back to the datagrid not updating after a record is added. I would have to click a button on the data pager (i.e. next page) in order for the datagrid to refresh and the record to appear within my data grid.

    So removing the old work around code doesn't resolve the issue cos the datagrid refresh does not seem to happen automatically when a record is added from a child window. So how do i resolve this?

    Regards, Shuja 

     

    Tuesday, December 08, 2009 9:48 AM
  • Ah, I understand now. I thought you were just updating a record, my fault for not reading the code correctly. My understanding of paging, the DDS, and the grid is that the paging system is not designed to handle dynamic changes in the page. One thought I have, if going to the next page shows the new record, can you just trigger the pager to go to the next page from code to refresh your grid?

    Tuesday, December 08, 2009 10:02 AM
  • No, not had any luck with trying to change the page in the datapager. I tried the following but i got an error about CanChangePage & CanLoad being false:

    this.SusPager.PageIndex = this.SusPager.PageCount;

    This seems like a simple task that i assumed everyone would need. I can't imagine that having a datagrid that does not refresh when a new record is added is good functionality. Why is it so difficult to get a solution for this in silverlight?

    Regards, Shuja 

    Tuesday, December 08, 2009 12:22 PM
  • It looks like you are adding the new entity into the DomainContext rather than into the DomainDataSource's DataView. DomainDataSource will only surface entities that were either loaded through it or added through it, so when you add the entity to the underlying DomainContext, it won't show up (as you're seeing). Check out DomainDataSource.DataView.Add; when you add to the DomainDataSource through that method, the entity will be reflected immediately. This page talks more about the DataView property and its API: http://jeffhandley.com/archive/2009/11/16/domaindatasourceview-again.aspx When you add an item through the DomainDataSource, and you have paging enabled, the added item will always be added to the current page. Because we cannot perform additional loads once you have pending changes (see http://jeffhandley.com/archive/2009/07/14/domaindatasourcesurvey.aspx for background), we don't want to let you push entities off the current page onto other pages when you have changes. So as items are added, the size of the current page will increase, and conversely, when you remove items, the size of the current page will decrease. Once you submit changes and reload the data, pages will return to their configured size.
    Tuesday, December 08, 2009 1:19 PM
  • Jeff mate, i think you solved the issue. It seems that what i was doing was adding the new record to the domain context and not the domain data source. I replaced my original code with the following and the datagrid refresh seems to work now:

    DomainDataSource dds = MyData;

    dds.DataView.Add(newsusrec.newsuslog);

    dds.SubmitChanges();

    Thanks for your help.

    Regards,

    Shuja

    Wednesday, December 09, 2009 7:17 AM
  • Hi, Why don't you try this: after adding the new record, just refresh your datagrid like so: MyContext..Add(); myGrid.ItemsSource = null; myGrid.ItemsSource = kasko.; Works just fine for me, even if I don't submit the changes
    Monday, December 14, 2009 10:34 AM
  • Hi, Sorry, the post didn't came out right.Here is the code again:
    MyContext.EntityList.Add(newEntity);
    myGrid.ItemsSource = null;
    myGrid.ItemsSource = MyContext.EntityList;
    Monday, December 14, 2009 10:36 AM
  • He did try that, mentioned it in the first post. However, that isn't a solution, that is a workaround and the pending change in the EntitySet is still going to  cause problems with the pagin if you don't do a SubmitChanges. Since Jeff wrote the DomainDataSource, his solution is, by definition, the right way to do it.

    Monday, December 14, 2009 11:02 AM
  • Is Jeffs' suggestion only for WCF Ria Services?  Because I can't find the Add method in the July preview.

    Monday, December 14, 2009 11:13 AM
  • Is Jeffs' suggestion only for WCF Ria Services?  Because I can't find the Add method in the July preview.

     

    Yes, Jeff is using parts of the DDS that didn't exist in previous versions.

    Monday, December 14, 2009 12:20 PM
  •  Hi Jeff,

    I did what you suggested in your response.

    source.DataView.Add(fed.NewFederation);
    source.SubmitChanges();

    However it does not work very well if you are using PageSize="5" on a grid, It will add it to the bottom of the grid making it 6 high?

    Do you have any suggestions

     

    Thanks

     

    Ade. 

    Monday, May 17, 2010 6:23 PM
  • Hi Ade,

    I replied to this issue on your other post: http://forums.silverlight.net/forums/t/182779.aspx

    That behavior is by design.

    Thanks,
    Jeff Handley

    Tuesday, May 18, 2010 2:45 PM