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