FOR lnCol = 1 TO This.ColumnCount loColumn = This.Columns[ lnCol ] FOR EACH loControl IN loColumn.Controls IF LOWER( loControl.BaseClass ) = 'header' BINDEVENT( loControl, 'Click', This, 'SortGrid' ) ENDIF ENDFOR ENDFORThis code in SortGrid()
LOCAL laEvents[ 1 ], loHeader, lcField, loColumn, lcSortOrder, loControl LOCAL llFoundColumn, llAllowCellSelection, lnRecNo, lcFrom, lnBuffering llAllowCellSelection = This.AllowCellSelection *** 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 IF EMPTY( lcField ) *** Try to find the field in the underlying data *** This code assumes that the *** The underlying cursor will be in natural order FOR lnCol = 1 TO This.ColumnCount IF This.Columns[ lnCol ].Name = loHeader.Parent.Name lcField = FIELD( lnCol, This.RecordSource ) EXIT ENDIF ENDFOR ENDIF This.cSortField = [] *** 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. 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 *** If the grid's RecordSource has pending changes, *** they are spurious and we can throw them away TABLEUPDATE( 1, .F., This.RecordSource ) CURSORSETPROP( "Buffering", 3, This.RecordSource ) ENDIF ZAP APPEND FROM DBF( 'qTmp' ) GO TOP IN ( This.RecordSource ) CURSORSETPROP( "Buffering", lnBuffering, This.RecordSource ) USE IN qTmp This.Refresh() *** And set the visual cues on the header loHeader.Picture = IIF( EMPTY( lcSortOrder ), [..\graphics\up.bmp], [..\graphics\down.bmp] ) loHeader.FontBold = .T. ENDIF ENDIF This.AllowCellSelection = llAllowCellSelection