Adding a sort to a field on a grid

Hi all, I have a grid on a form (InventJournalCount), that the users wish to sort by the ItemID. Currently there is no sort on the grid. I understand that I need to use the AddSortField command, but I’m not sure where to put it (possibly the Init method on the Form). What do you think?

Many thanks,

Anna

It might be something like this…

InventJournalTrans_ds.query().dataSourceTable(tablenum(InventJournalTrans)).addSortField(fieldnum(InventJournalTrans, ItemID), SortOrder::Descending);

I’m not sure where to put the code though. The query part throws me as well. Is that used only if there is a query as the datasource?

I am using the InventJournalTrans and the InventDim tables as the datasources for this form.

Anna

Ask yourself when it should and can run. It should run when you start the form, but it can’t run before the query is initialized. Therefore the first possible place is init() method of the datasource after super().

It could look like this:

this.queryBuildDataSource().addSortField(...);

Form’s init() method is another possible place.

In many cases, you can simply set Index property on the datasource and you don’t have to write any code at all.

Form always uses a query, if it has at least one datasource. Whether you add tables directly or from an AOT query isn’t important.

Thanks for the quick reply Martin!

I opened up the Init method on the form itself, and added the code in my first post. Do I need to add it before or after the super() call?

Many thanks,

Anna

super() calls the kernel implementation of init(), which is where datasource get initialized. You have to first create datasources (super()) and only then you can modify them.

Thanks Martin. I added it after super(), saved and compiled (with no errors). I went back into the Counting Journal lines screen, and it is still sorted in the wrong order, so I must be doing something wrong. Do you think that my code is correct?

Anna

To debug a query, first look at the (pseudo)-SQL query string that you managed to generate by your code. For example, put info(InventJournalTrans_ds.queryBuildDataSource().toString()) after adding your sorting.

Thanks Martin! You’re a star! :o) The problem was that my order was set to descending instead of ascending. Once I changed that, everything worked as expected :o) Thanks again for your help!!

Anna