refresh after save of a form resets the selected item RRS feed

  • Question

  • is Light Switch

    my app main screen has a search list for customers and the selected one displays on the right side the details of the customer

    adding a new customer is done in another screen

    when returning to the main screen(customer search) the new inserted customer is not visible until refresh, but refresh sets the list at index 0 and  the new customer is not visible at all times, so you'll have to search for it

    I need for the user to see this customer without further clicks & searches

    one idea was to select programatically this customer in the list after refresh, but I could'nt find the way to do this


    Tuesday, November 1, 2011 7:04 PM

All replies

  • There is a way, but it's not very elegant. You can store the new id to the application and then retrieve it after refresh. I can post a sample if you like.

    1+1 = 3 for large values of 1
    Tuesday, November 1, 2011 7:09 PM
  • I did store the new id in application.cs as a public prop

    but I do'nt know what to do with it

    I ca'nt seem to find the way to make a selection in the list by code

    the example would be very welcomed


    1+1=4 for huge values of 1 or tiny values of 4

    Tuesday, November 1, 2011 7:15 PM
  • I would make the property nullable
    public int? LastCustomerId {

    Then in the search screen the easiest way would be to override the code for Refresh to something like this: 
    (F.Y.I. As I cannot write code right now I don't know what paging could cause to the integrity of this approach) 

    partial Refresh_Execute(){
      if (this.Application.LastCustomerId.HasValue){
        customersQuery.SelectedItem = 
        this.Application.LastCustomerId = null;

    1+1 = 3 for large values of 1
    Tuesday, November 1, 2011 7:33 PM
  • I ca'nt find the Refresh_Execute() in the 'Write code' of the screen

    so I did the following to test

    partial void CustomersByStateListDetail_Saved()



    if (Application.selectedID.HasValue){

    CustomersByState.SelectedItem = CustomersByState.Where(t => t.idTercero == Application.selectedID.Value).FirstOrDefault();



    did'nt work

    it still selects the first item in the list

    Tuesday, November 1, 2011 8:51 PM
  • Just an idea why it is not working.

    If you have paging enabled in the query then maybe, according your query sort and filter criteria, your screen collection doesn't include the id you specified in the first page it loads. Your screen collection doesn't include all database records when paging is enabled. Try to disable paging in the screen collection query and check again the code. Maybe that helps.

    Wednesday, November 2, 2011 5:43 AM
  • Another option is sort the query decending by CreatedDate on the customer record. That way, last customer added is always first customer in list after a refresh and you can also keep paging.
    Wednesday, November 2, 2011 6:59 AM
  • Paging I also suggested as a possible issue in my first post, like George suggets.

    First you have to check if the query returns something. Break your code to something like this:


    Customer customer = CustomersByState.Where(t => t.idTercero == Application.selectedID.Value).FirstOrDefault();
    CustomersByState.SelectedItem = customer;


    And see if the customer has a value. If it does then by 99% it's that your query (as also George suggests) does not contain the customer. I see the name of the query is CustomerByState. I can only assume that either your customer is not from the state defined or not in the page you currently display. As disabling paging is not something I can suggest, I am afraid you will have to find a way to locate the page where your customer is. One idea would be to store along with your customer's id a required field value (e.g. LastName) add to your query an optional parameter like LastName >= @LastName and if you have a new customer pass the last name of the customer to the parameter. If you also sort with this field, not only your customer will be selected but it will also be "top of the heap".

    About overring the Refresh it was my mistake. Save provides the ability to override code. For some reason Refresh command does not. So another place you can put your code is in CustomerByState_Loaded that we be called after every query execution and thus after Refresh also.

    William's idea is also a good one as long as you have auditing information stored along with your entity (Creation/Modification Date, User etc).

    1+1 = 3 for large values of 1
    Wednesday, November 2, 2011 7:14 AM
  • hola

    Thank you for the answers

    I tested editing a customer that is not first on the list

    since the refresh code & the selection atempt is made in the 'saved' routine it should have stayed selected after save

    one more thing, this was on the first page shown and none of the filters was changed

    none the less after saving (+refresh+select) the selected customer will be the first on that page even if the one I edited & tryed to select is on that same page & visible(so no scroll was needed)

    sorting by creation date might be a solution, it still involves the user getting a list ordered in a way not so easy to understand (versus alphabetical ,as it is now)

    Wednesday, November 2, 2011 12:44 PM