locked
Screen with two grids and wrong behavior RRS feed

  • Question

  • My screen is made of two grids and holds entities of clients.

    The first grid fetches all the clients from the database and the second grid fetches clients that have birthday in any date of this month.

    When i open the screen everything is ok, the problem starts when i try to insert a new client in my first grid. In that case if i press the plus sign and the auto create form comes up, after i fill the fields and press ok i see the new created client in the two grids. Of course after i press save the clients that have not birthday this month are gone from that grid but i wonder why in first place i see them there since i have created a new client in the other grid. Plus this two grids are filled with two separate queries.

    Any thoughts?

    Tuesday, October 18, 2011 2:54 PM

Answers

  • This is actually the designed behavior.

    The reason is that the queries which the data grid are bound execute server-side. If you add an entity on the client and have not saved yet, there is no good way to tell if the entity matches the query's criteria without saving and executing the query again. It gets even more tricky when you think about more complex scenarios. For example, what if a query states that the date value of a field must be within a certain range and the user changes the date value of that field on one entity that initially passed the filter but would no longer because the date has been changed to something outside the range. Would it not be strange that the entity would just all of a sudden disappear?

    A great way to get around this seemingly odd behavior is to change the workflow for that screen a little bit. Instead of allowing the user to add new entities on that screen, create a New Data screen which will be used to allow the user to add the new entities. Once the New Data screen is saved, have it close itself the refresh the collections on the "parent" screen. This way, the user never sees added entities on the "parent" screen. You might implement a similar workflow for editing entities as well.


    Justin Anderson, LightSwitch Development Team
    Wednesday, October 26, 2011 8:26 AM
    Moderator

All replies

  • This is actually the designed behavior.

    The reason is that the queries which the data grid are bound execute server-side. If you add an entity on the client and have not saved yet, there is no good way to tell if the entity matches the query's criteria without saving and executing the query again. It gets even more tricky when you think about more complex scenarios. For example, what if a query states that the date value of a field must be within a certain range and the user changes the date value of that field on one entity that initially passed the filter but would no longer because the date has been changed to something outside the range. Would it not be strange that the entity would just all of a sudden disappear?

    A great way to get around this seemingly odd behavior is to change the workflow for that screen a little bit. Instead of allowing the user to add new entities on that screen, create a New Data screen which will be used to allow the user to add the new entities. Once the New Data screen is saved, have it close itself the refresh the collections on the "parent" screen. This way, the user never sees added entities on the "parent" screen. You might implement a similar workflow for editing entities as well.


    Justin Anderson, LightSwitch Development Team
    Wednesday, October 26, 2011 8:26 AM
    Moderator
  • I can understand what you are saying but please read the following case.

    I have two entity queries on my screen. ActiveClients and ThisMonthBirthdayClients.

    Although this two collections where generated at server side it is reasonable at run time to write something like ActiveClients.Add(newClient) and expect this statement to add a new client into the ActiveClients collection only. The auto created form for adding entities is not executing this statement, or a similar statement after you press the OK button?

    I can hardly believe that it seeks the whole collections in the screen to determine the collection's entity type and add to each collection the newly created entity. If this is the case, i think that it is wrong.

    What you believe?

     

    Wednesday, November 2, 2011 5:56 AM
  • Hi George,

    I believed Justin is trying to explain a default characteristic of LS when adding new data in a screen.  Remember that screens are only interfaces on the client side used to communicate in the server side.  Have ever tried pressing refresh after adding new record?  The refresh button should show your newly added data in your grid.

    Although this two collections where generated at server side it is reasonable at run time to write something like ActiveClients.Add(newClient) and expect this statement to add a new client into the ActiveClients collection only. The auto created form for adding entities is not executing this statement, or a similar statement after you press the OK button?

    If you are using the default modal window in adding new record, I don't see any reason why this doesn't work in your screen.  Not unless you have created you own customized modal window for adding new record.  If you do, you can refer to the post of Yann Duran http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/138464dd-f38e-48b6-a610-28279418b238 or http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/138464dd-f38e-48b6-a610-28279418b238 by Sheel Shah.

     

    Hope this helps.

     

    Rkage 


    • Edited by RKage Wednesday, November 2, 2011 11:12 AM
    Wednesday, November 2, 2011 11:11 AM
  • Hi RKage.

    The problem is not Refresh and i completely understand and agree with Justin's thought. The collections are generated at server's side and are served to the client. But at the end they are two different collections.

    The behavior that is puzzling me is before i press Save. If you read my first post you will see that i have two same entity collections in a screen but they are generated with different criteria. When i press Add New on the top of one of the two grids i expect the row to be added only to the grid that i pressed the button and not to both of them. It puzzled the user when it show it and it also puzzled me when i was informed of this.

    It is really very easy to reproduce this behavior and i am just wondering why this is happening, what is the logic behind. This two collections are two separate collection properties in the ViewModel context so why when i add a new entity in one of the two entities LightSwitch adds it to the other one also? I think this is not right.

    Why? First of all i see an entity to a grid that i haven't pressed add or write code to add an entity there. Secondly and more importantly even after i press Save the entity is still there in the wrong grid that it shouldn't be. Only Refresh can correct this problem.

    Someone said, LightSwitch is an excellent product, it makes hard things easy and easy hard. Go figure...

    Wednesday, November 2, 2011 12:49 PM
  • LightSwitch knows that the underlying data in both your grids comes from the same source table so new items show up in both grids despite one of them being filtered (as explained by Justin). Items added to one will automatically show in the other because they are based on the same underlying objects.

    If you don't want this behaviour, try having a separate data source for the filtered grid.


    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.
    Wednesday, November 2, 2011 12:58 PM
  • Thank you for your answer Simon.

    Having a different data source is an option i agree. So what you are telling me is that when i press the OK button on Add New Client the code behind instead of executing a statement like Screen.ActiveClients.Add(newClient) or similar it executes a statement like Me.DataWorkspace.DatatSourceName.Clients.Add(newClient) and because of that i see the newly created client in both grids. Maybe the statements are not exactly like that but you understand the meaning.

    Well i can understand the logic now and it is more clear to me but i still wonder why they choose this approach. For a single user application that the refresh has only the meaning of cancelling changes, requires from the user to press refresh after every save so that the records in the grid being correct.

    Imagine for example that the second list wasn't ThisMonthBirthdayClients but ClientsWithDebpt or ClientsWithPendingTickets or whatever and above the grid i had enabled the export to excel option.

    Although i understand the logic i find my self hard to believe that this is a good approach but have to take as it is.

     

     

    Wednesday, November 2, 2011 5:29 PM
  • I think you're sort of right.

    I believe that both the collections you see are not in fact separate collections but are both built on top of a single collection of the underlying data. Adding to one adds to the underlying collection which then shows up in the other.

    The reason the grids work the way they do was explained by Justin - basically it would not be sensible for data you just entered in a grid to disappear from view when you move off the "new row" just because it didn't meet the filter criteria. You would have no confidence that the data had been entered into the application correctly and you would have no way to change your mind and edit the data before saving it.

    Add to this the fact that LightSwitch's designers probably didn't think you'd have two grids of the same data (one filtered, one not) on screen at the same time and you get to the position we are in today.

    The user or application shouldn't have to refresh the screen except to cancel an edit or see data entered on other screens or by other users. Using Refresh in the way you're doing is just a workaround for the problem of having two grids tied to the same data with different filter criteria.

    Try using two different data sources, even in a simple test application, to see if it is a better solution.


    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.
    Wednesday, November 2, 2011 11:07 PM
  • I agree with both of you.  In LS you can do workarounds to achieve what you want, but don't you think very illogical to deal with identical data in two different table  or dealing with two identical table in one screen?

    @The first grid fetches all the clients from the database and the second grid fetches clients that have birthday in any date of this month.

    When i open the screen everything is ok, the problem starts when i try to insert a new client in my first grid. In that case if i press the plus sign and the auto create form comes up, after i fill the fields and press ok i see the new created client in the two grids. Of course after i press save the clients that have not birthday this month are gone from that grid but i wonder why in first place i see them there since i have created a new client in the other grid. Plus this two grids are filled with two separate queries.

    I can see that your are worried about the appearance of the newly added data in the grid though in the first place it should not be not unless it meets the query criteria.  Whay don't you try Justine's suggestion?

    "A great way to get around this seemingly odd behavior is to change the workflow for that screen a little bit. Instead of allowing the user to add new entities on that screen, create a New Data screen which will be used to allow the user to add the new entities. Once the New Data screen is saved, have it close itself the refresh the collections on the "parent" screen. This way, the user never sees added entities on the "parent" screen. You might implement a similar workflow for editing entities as well."

     I have used this approach before by following Beth Massi's blog on http://blogs.msdn.com/b/bethmassi/archive/2011/08/16/how-to-allow-adding-of-data-to-an-auto-complete-drop-down-box-in-lightswitch.aspx  the only thing here is that,it focuses on an ACB, but the logic of adding new record presented by Beth, I believe, is very appropriate to your issue.

    Thursday, November 3, 2011 12:25 AM