in a recent thread about sorting a grid by clicking its headers, i explained a procedure to do that. i think posting it in my blog could be useful for others.

the thread post is here.

i improved the code and there is a link below to download a sample form.

you should have 2 icons in your project, one for descending (a small arrow downward) and the other for ascending (small arrow upward).

in the init event or at design time, add the appropriate icon to the appropriate header(picture property) so that the correct sorting type on the correct index is reflected when you first show the form.

the cursor that is the source of the grid should be indexed on all the fields you want to sort on (by using index on ... ).
for the code in the header click event to work, it need that when you define indexes start in the same order of columns that the grid start with, so that the first index on code will create index on the first field column. this is because (it is easy for me) i will use index numbers to deal with sorting. then you set the order to the index you put the picture in its column header. this way you can just copy paste the code you put in column header1 click to all other headers and just change the index number in the code to reflect the underlying field/column to sort on.

the code of the first header click event will be like this:

** first column header click event
select tempstudents
if val(sys(21))<>1  && current index number
  set order to 1 in tempstudents
  if descending(1,"tempstudents")
    set order to 1 in tempstudents ascending

    set order to 1 in tempstudents descending


this.picture=iif(descending(1,"tempstudents"), thisform.picdesc, thisform.picace) && the sorting icon

go top in tempstudents

in the header of column2, just change the (one)s in the code to 2 and so on for the other headers.

download the sample and study it to be more oriented about the idea.

here is a picture of the sample form:

and here is the sample form to download:

best regards

One Response to Sorting the Grid by Header Clicking Asc / Dec

  • 3rn3st0 says:

    A simple, practical and elegant solution. Thank you!

    -Thanks … you can use Bindevent() also to bind the header clicks to one method so that you can use write the code once rather than in every header click. .. Ammar


Leave a Reply

Your email address will not be published. Required fields are marked *