StrataFrame Forum

UI Databinding Problem with BO Sort property

http://forum.strataframe.net/Topic18318.aspx

By Aaron Young - 8/4/2008

Hi,

I have a strange problem because of one line of code and I can't see why. Basically, when I use the BO.Sort method it causes the UI refresh of the BO bindings to fail when a new record is added. When the BO.Sort line is removed, it works correctly.

I have a very simple SF maintenance form with a single BO and a single textbox bound to a field. In the ParentFormLoading I have the following code:-

            bo1.FillAll();

            // Set sort order
            bo1.Sort = "CODE";
            bo1.Navigate(MicroFour.StrataFrame.Business.BusinessNavigationDirection.First);

When the form loads the textbox correctly shows the first record and the navigation tools work correctly. However, when a new record is added the textbox always shows the value from the very first record instead of the new blank record - regardless of which record I was on at the time the New button was clicked. For example, if I navigate to the 10th record and click New, the textbox suddenly shows the value from the 1st record. When I click Save with no data entry, the textbox shows an empty value and for the first time the UI is set to the new record - it looks like the UI databindings are only refreshed during the Save operation.

I have checked in the AfterAddNew BO method and the new record is correctly added and is the current record. However, the UI always shows the very 1st record until the Save is actioned.

When I remove the bo1.Sort line the problem goes away.

I can build the sort into the BO Fill SQL command but I would be interested to know what I have done wrong.

Thanks in advance,

Aaron

By Dustin Taylor - 8/4/2008

This is happening since your BO is sorting on the primary key of the table. Whenever you do an add, it is adding a new record with a "-1" primary key which, when sorted, gets immediately stripped out of the CurrentView. w00t



The safest way to get around this is to disable your sort right before the add, then re-enable it right after.



bo1.Sort = "";

bo1.Add();

bo1.Sort = "CODE";




Alternatively, you could sort on something other than the primary key, but even then you could get some funky behavior depending on what you choose to sort on.



The key here is that a sort or filter of a business object will be immediately applied whenever you add or remove a record, and, as such, any filters or sorts should always be taken into account when modifying the contents of the BO.



There a bunch of forum topics out there on this particular subject covering everything from alternative work arounds to why we do it this way, but that should at least point you in the right direction Smile.
By Ivan George Borges - 8/4/2008

Hey Aaron.

Some links that might help you:

http://forum.strataframe.net/FindPost18218.aspx

http://forum.strataframe.net/FindPost11815.aspx

Hope it helps. Wink

By Aaron Young - 8/4/2008

Thanks Dustin and Ivan.

That helps to explain it. I think I will stop using the Sort method and hard code the sort order directly in the Fill() method of the BO as it sounds like that will work.

Thanks again for the help.

By Dustin Taylor - 8/5/2008

Yep, that would work great. Smile