Now any grid column header can be clicked in ascending order. Immediately if you click again on the same column header it will be arranged in descending order.
Unicode arrow characters are suitably added to the column headers.
Claudio: Can we have a property to find out ColumnHeadClicked just like CellColClicked?
Code: Select all
#include <hmg.ch>
*----------------------------------------------------------------------*
PROCEDURE SortGrid( cWindow , cGrid , nColumn, cSortAs, lDesc )
*----------------------------------------------------------------------*
LOCAL L
LOCAL I
LOCAL aGrid := {}
local cHeader := ''
local nColCount := 0
local aJustify := {}
local aSorted := {}
default lDesc := .f.
default cSortAs := 'C'
default nColumn := 1
domethod( cWindow, cGrid, 'DISABLEUPDATE' )
cHeader := getproperty( cWindow, cGrid, 'ColumnHEADER', nColumn )
if HB_URIGHT( cHeader, 2 ) == ' ▲' // already in ascending order. change to desc!
lDesc := .t.
endif
if HB_URIGHT( cHeader, 2 ) == ' ▼' // already in descending order. change to asc!
lDesc := .f.
endif
nColCount := getproperty( cWindow, cGrid, 'COLUMNCOUNT' )
for i := 1 to nColCount
cHeader := getproperty( cWindow, cGrid, 'ColumnHEADER', i )
if HB_URIGHT( cHeader, 2 ) == ' ▲' .or. HB_URIGHT( cHeader, 2 ) == ' ▼' // remove the exising arrow
cHeader := hb_usubstr( cHeader, 1, hmg_len( cHeader ) - 2 )
setproperty( cWindow, cGrid, 'COLUMNHEADER', i, cHeader )
endif
next i
WAIT WINDOW 'Sorting...' NOWAIT
* Copy the grid content to an array.
L := GetProperty ( cWindow , cGrid , 'ItemCount' )
FOR I := 1 TO L
AADD( aGrid , GetProperty( cWindow , cGrid , 'Item' , I ) )
NEXT I
* Then Sort The Array...
if lDesc
do case
case cSortAS == 'C'
aSorted := asort( aGrid, , , { |x, y| x[nColumn] > y[nColumn] } )
case cSortAS == 'N'
aSorted := asort( aGrid, , , { |x, y| val( x[nColumn] ) > val( y[nColumn] ) } )
case cSortAS == 'D'
aSorted := asort( aGrid, , , { |x, y| ctod( x[nColumn] ) > ctod( y[nColumn] ) } )
endcase
else
do case
case cSortAS == 'C'
aSorted := asort( aGrid, , , { |x, y| x[nColumn] < y[nColumn] } )
case cSortAS == 'N'
aSorted := asort( aGrid, , , { |x, y| val( x[nColumn] ) < val( y[nColumn] ) } )
case cSortAS == 'D'
aSorted := asort( aGrid, , , { |x, y| ctod( x[nColumn] ) < ctod( y[nColumn] ) } )
endcase
endif
* Finally, Fill The Grid With The Sorted Array Content...
DoMethod( cWindow , cGrid , 'DeleteAllItems' )
FOR I := 1 TO L
DoMethod( cWindow , cGrid , 'AddItem' , aSorted[I] )
NEXT I
cHeader := getproperty( cWindow, cGrid, 'ColumnHEADER', nColumn )
if lDesc
cHeader := cHeader + ' ▼'
else
cHeader := cHeader + ' ▲'
endif
setproperty( cWindow, cGrid, 'COLUMNHEADER', nColumn, cHeader )
domethod( cWindow, cGrid, 'ENABLEUPDATE' )
* End
WAIT CLEAR
RETURN