Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

How can I set the sort order on a field on a grid in a form

(0) ShareShare
ReportReport
Posted on by 4,049

I have a problem with sorting lines in a Grid on a form in D365FO.

The form is not a standard form but one made custom for our solution.

And the problem is that since we need to sort on a field in the Grid that is a Reference Group field, and because of that it was not possible to sort on the data source.

But we found that it was possible for the user to sort on that field in the client by choosing "Sort A - Z", and from that we found that from code we could call the .Sort method on the field with parameters SortOrder::Ascending

But it was not really clear where the best place was to call the .Sort method, so to start with it was placed in the task method on the form that was already used, and that did seem to work.
But if the user then edits some data in the form and press the save button, then the Sorting seems to go back to the old sorting.

So our problem is where should we place the call to the .Sort method to make sure it works?

  • Alex VN Profile Picture
    1,994 on at
    RE: How can I set the sort order on a field on a grid in a form

    Hi,

    In this case I think it would be better to check for that datasource query to make it persistent, and then you might add sort order to the query instead of calling the sort method on the datasource as the sort method might call active method and get stuck in infinite loop.

    You might refer to this article:

    community.dynamics.com/.../dynamics-ax-2012-form-datasource-sort-x

    Regards,

  • Rudi Hansen Profile Picture
    4,049 on at
    RE: How can I set the sort order on a field on a grid in a form
    [quote user="Alex VN"

    Hi,

    If you want it to be persistent I think you can try to do the sort on respective Datasource - Active method, which will be called everytime you change the record cursor.

    Regards.

    [/quote

    Good sugestion, but when I do that it seems to get caught in a loop where the active method is called again and again.

    So  it would seem that calling the Sort method on the field then also makes the form call the active method again.

    I did try checking the value of the Sort before setting it like this:

    if(RetailGroupMemberLine_Product.sort() != SortOrder::Ascending)
    {
        RetailGroupMemberLine_Product.sort(SortOrder::Ascending);
    }
    

    But it still keeps calling the active motode in a loop.

  • Alex VN Profile Picture
    1,994 on at
    RE: How can I set the sort order on a field on a grid in a form

    Hi,

    If you want it to be persistent I think you can try to do the sort on respective Datasource - Active method, which will be called everytime you change the record cursor.

    Regards.

  • Rudi Hansen Profile Picture
    4,049 on at
    RE: How can I set the sort order on a field on a grid in a form

    No I need a code solution for the problem, like where do I call the .Sort method so it will always be active on the field.

  • BillurSamdancioglu Profile Picture
    17,153 Most Valuable Professional on at
    RE: How can I set the sort order on a field on a grid in a form

    Hi,

    You may sort the form from grid and then save the form and apply for all the users?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,255 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,984 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans