none
problem refreshing grid

    Question

  • Hi

    I have a grid with a custom "add new" button on it. This loads an add new form.  When the form is saved the grid does not show the new data.

    I have tried refreshing the entity when the form closes like this:

    partial void AddNewConsultant_Closing(ref bool cancel)
            {
                this.co_Consultants_Roles.Refresh();
                
    
            }

    But it doesn't refresh the grid until I click the refresh button.

    How do I refresh the screen in code?  I am baffled by some of the examples I've found. I don't need to iterate dozens of screens. Just refresh the grid.

    Gus

    Friday, May 25, 2012 10:43 AM

Answers

  • As each screen in a LightSwitch application effectively shows its own copy of the data, this behaviour is by design.

    Adding code to refresh a collection when closing the form will not make any difference as this form refreshes its own data and then closes without affecting any other form.

    The easiest way to program round this is the add the code to refresh the collection to the Activiated method of the screen where you want to see the changed data (the grid). This will, however, refresh the data whenever the user goes to that screen, not just after saving data and closing the Add New screen. To avoid this, add a public boolean variable (RefreshData) to your other form and set this in your Closing event on the Add New form. Check this variable in the Activated event of the grid form and refresh the collection if it is set. Remember to reset the variable to False so you don't refresh unnecessarily if the user just switches away from the grid form and comes back without having added any new data.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by babloo1436 Friday, May 25, 2012 12:14 PM
    • Marked as answer by GusBeare Friday, May 25, 2012 12:59 PM
    Friday, May 25, 2012 11:50 AM

All replies

  • As each screen in a LightSwitch application effectively shows its own copy of the data, this behaviour is by design.

    Adding code to refresh a collection when closing the form will not make any difference as this form refreshes its own data and then closes without affecting any other form.

    The easiest way to program round this is the add the code to refresh the collection to the Activiated method of the screen where you want to see the changed data (the grid). This will, however, refresh the data whenever the user goes to that screen, not just after saving data and closing the Add New screen. To avoid this, add a public boolean variable (RefreshData) to your other form and set this in your Closing event on the Add New form. Check this variable in the Activated event of the grid form and refresh the collection if it is set. Remember to reset the variable to False so you don't refresh unnecessarily if the user just switches away from the grid form and comes back without having added any new data.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by babloo1436 Friday, May 25, 2012 12:14 PM
    • Marked as answer by GusBeare Friday, May 25, 2012 12:59 PM
    Friday, May 25, 2012 11:50 AM
  • Excellent, thanks, that did the job.

    I am using c# so I can't use public variables.  But I found I could add a public class to the Common/UserCode folder to hold the variable:

     public static class ReloadYN
        {
            private static Boolean _ReLoad;
    
            public static Boolean ReLoad
            {
                get { return _ReLoad; }
                set { _ReLoad = value; }
            }
    
    
        }

    Then before closing the screen in the screen saved event I put this:

                // set a public variable to true so we only refresh the grid after this
                ReloadYN.ReLoad = true;
    
                // close the screen
                this.Close(true);

    And then catch it in the grid activate:

      partial void ConsultantRolesGrid_Activated()
            {
                //check if we should re-load the screen
                if (ReloadYN.ReLoad == true)
                {
                    // refresh the screen and flag that we don't need to refresh again until asked
                    this.co_Consultants_Roles.Refresh();
                    ReloadYN.ReLoad = false;
                }
    
    
            }

    hope this helps someone else later

    thanks again

    Gus

    Friday, May 25, 2012 12:59 PM
  • Gus,

    I use a bool at the application level,

    e.g.  public bool mycollectionrefresh;

    in the application.cs file.  Set it in one screen and use the "Activated" method to check the bool.  This avoids having to create an entire class.  I think that's what Simon meant.

    Scott

    Friday, May 25, 2012 4:03 PM