Foxite.COM Community Weblog

Foxite.COM Community Weblog - free weblog service for the Visual FoxPro Community.
Welcome to Foxite.COM Community Weblog Sign in | Join | Help
in
Home Blogs Forum Photos Forum Archives

Ammar Hadi



Sorting the Grid by Header Clicking Asc / Dec


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
ELSE
  IF DESCENDING(1,"tempStudents")
    SET ORDER TO 1 IN tempStudents ASCENDING

  ELSE
    SET ORDER TO 1 IN tempStudents DESCENDING

  ENDIF
ENDIF

THIS.PARENT.PARENT.SETALL("picture","","HEADER")
THIS.PICTURE=IIF(DESCENDING(1,"tempStudents"), THISFORM.picdesc, THISFORM.picace) && the sorting icon

GO TOP IN tempStudents
THIS.PARENT.PARENT.REFRESH
THIS.PARENT.SETFOCUS

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:

http://www.foxite.com/uploads/5c545b64-b731-4c6f-bae4-4e20ed0c6c5b.rar

Best Regards

Published Friday, June 19, 2009 7:48 PM by AmmarHadi

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

3rn3st0 said:

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

 

July 14, 2009 6:48 PM

What do you think?

(required) 
(optional)
(required) 

This Blog

Post Calendar

<June 2009>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

Post Categories

News

June 2009 - Iraqi Visual FoxPro Programmers Group established.

Syndication