Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grid Header BindEvent

Status
Not open for further replies.

dcrooks

Programmer
Aug 8, 1999
12
US
Has anyone used BindEvent to add to the grid header the ability to click the header to sort a column?

I have the latest FoxPro Advisor which has an article by Drew Speedie that does what I want but I don't want to retype all the code and I am not really sure all of the pieces are there.
 
As you can tell from the article, it's not trivial (although make sure you read to the end!). I believe the code is available for download if you are a subscriber.

Other's have created class code without the BindEvents to accomplish the same thing, and that code is in a lot of places - the UT, CompuServe and um.... here! (I believe n the FAQs.)

Rick
 
Dcrooks,

Has anyone used BindEvent to add to the grid header the ability to click the header to sort a column?


Yes. I read Drew's article last week, and it inspired me to do the same.

Unfortunately, I can't offer you any code. That's because my method is totally non-generic. For reasons I won't go into, I was working with an existing form which contains a base grid, and so I had to add the relevant code to the form itself. In other words, it's not a class.

You'd be better off with Drew's code. If you don't want to type it in, perhaps you can download it from
Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Hello dcrooks.

This is my sortGrid method. I use BindEvent in the grid's Init() to bind the header click to the SortGrid() method:

Code:
LOCAL laEvents[ 1 ], loHeader, lcField, loColumn, lcFrom, lnBuffering, lcSortOrder, loControl
LOCAL llFoundColumn, llAllowCellSelection

*** First of all, see which column fired off this event
AEVENTS( laEvents, 0 )
loHeader = laEvents[ 1 ]
IF VARTYPE( loHeader ) = 'O'
  *** First See if a ControlsSource was set for the column
  WITH loHeader.Parent
    lcField = ''
    IF NOT EMPTY( .ControlSource )
      *** Cool. Use it to decide how to sort the grid
      IF NOT EMPTY( .ControlSource ) AND ( '.' $ .ControlSource ) AND NOT( '(' $ .ControlSource )
        lcField = JUSTEXT( .ControlSource )
      ENDIF
    ENDIF
  ENDWITH
  *** we have a field - let's see if it already has a sort order set
  *** if it does, it will have the appropriate picture in the header
  lcSortOrder = ''
  IF NOT EMPTY( loHeader.Picture )
    lcSortOrder = IIF( LOWER( JUSTFNAME( loHeader.Picture ) ) == 'down.bmp', '', 'DESC' )
  ELSE
    *** See if there is a visual cue on any of the other grid
    *** column headers and remove it if there is
    FOR EACH loColumn IN This.Columns
      FOR EACH loControl IN loColumn.Controls
        IF LOWER( loControl.BaseClass ) == [header]
          IF NOT EMPTY( loControl.Picture )
            llFoundColumn = .T.
            loControl.Picture = []
            loControl.FontBold = .F.
            EXIT
          ENDIF
        ENDIF
      ENDFOR
      IF llFoundColumn
        EXIT
      ENDIF
    ENDFOR
  ENDIF
  
  *** if we have a field - let's sort
  IF NOT EMPTY( lcField )
    *** There seems to be a refresh issue here
    *** because even though the data is in the cursor
    *** it is not showing up in the grid after the sort
    *** and it looks like it is related to AllowCellSelection being .F.
    llAllowCellSelection = This.AllowCellSelection
    This.AllowCellSelection = .F.
    This.Refresh()
    KEYBOARD '{CTRL+TAB}'
    lcFrom = This.RecordSource + [ ORDER BY ] + lcField + [ ] + lcSortOrder + [ INTO CURSOR qTmp NOFILTER]
    SELECT * FROM &lcFrom
    SELECT ( This.RecordSource )
    lnBuffering = CURSORGETPROP( "Buffering" )
    IF lnBuffering > 3
      CURSORSETPROP( "Buffering", 3, This.RecordSource )
    ENDIF
    ZAP
    APPEND FROM DBF( 'qTmp' )
    GO TOP IN ( This.RecordSource )
    CURSORSETPROP( "Buffering", lnBuffering, This.RecordSource )
    USE IN qTmp
    *** And set the visual cues on the header
    loHeader.Picture = IIF( EMPTY( lcSortOrder ), [..\graphics\up.bmp], [..\graphics\down.bmp] )
    loHeader.FontBold = .T.
    This.AllowCellSelection = llAllowCellSelection
    This.Refresh()
    This.SetFocus()
  ENDIF
ENDIF


Marcia G. Akins
 
Thanks for the code Marcia!

I did find the code on the Advisor website. I just had the impression from the article that only the CD had the code. I now sit corrected. :cool:

"Use the Right Tool for the Job!"
David L. Crooks
Anteon Corporation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top