How to ensure a particular row stays at the bottom of a grid/list during sort

Date:13 June 2012
Product/Release:Visual LANSA V11 SP5 or later
Abstract:One method for ensuring a particular row (i.e. Totals Row) stays at the bottom of a grid, while other rows can be sorted
Submitted By:LANSA Technical Support

The following sample code describes one method of how how to might implement a grid with a totals row, that allows sorting for all rows while keeping the totals row at the bottom. It is achieved by having a hidden column and manually assigning sort order based column header click events.

Create an RDMLX form with the following code:

* **************************************************
* 
* COMPONENT: STD_FORM
* 
* **************************************************
Function Options(*DIRECT)
Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(266) Clientwidth(400) Height(304) Left(24) Top(126) Visible(False) Width(416)
Define_Com Class(#PRIM_GRID) Name(#GRID_1) Captionnoblanklines(True) Columnbuttonheight(18) Columnbuttonpress(True) Columnscroll(False) Componentversion(1) Displayposition(1) Height(249) Left(8) Parent(#COM_OWNER) Showselection(True) Showselectionhilight(False) Showsortarrow(True) Tabposition(1) Top(8) Width(377)
Define_Com Class(#PRIM_GDCL) Name(#GDCL_1) Displayposition(1) Parent(#GRID_1) Source(#STD_CODE) Width(25)
Define_Com Class(#PRIM_GDCL) Name(#GDCL_2) Displayposition(2) Parent(#GRID_1) Source(#STD_CODEL) Width(31)
Define_Com Class(#PRIM_GDCL) Name(#GDCL_3) Displayposition(3) Parent(#GRID_1) Source(#STD_COUNT) Width(21) Widthtype(Remainder)
* The STD_NUM field is the hidden column that is used to determine which row is displayed last
Define_Com Class(#PRIM_GDCL) Name(#TotalSort) Parent(#GRID_1) Sortposition(1) Source(#STD_NUM) Visible(False)

Evtroutine Handling(#com_owner.Initialize)
Set Com(#com_owner) Caption(*component_desc)

* Make some records
Clr_List Named(#GRID_1)
Begin_Loop Using(#STD_ENTRY) To(12)
#STD_CODE := #STD_ENTRY.AsString
#STD_CODEL := (#STD_ENTRY * 100).AsDate( MMYY ).AsDisplayString( MMMMMMMMMM ).Leftmost( 3 )
#STD_COUNT := #STD_ENTRY
#STD_NUM := 0
Add_Entry To_List(#GRID_1)
End_Loop

* Make a record for Total
#STD_CODE := TOT
#STD_CODEL := '5'
#STD_COUNT := 100
#STD_NUM := 1
Add_Entry To_List(#GRID_1)

Endroutine


Evtroutine Handling(#GDCL_1.Click #GDCL_2.Click #GDCL_3.Click) Options(*NOCLEARMESSAGES *NOCLEARERRORS) Com_Sender(#RowHeader)
* Make sure the hidden column is the first sort position
#TotalSort.SortPosition := 1

* Is it already sorted by this column?
If Cond((#RowHeader *As #PRIM_GDCL).SortPosition = 2)
* If so, we just change the sort direction
If Cond((#RowHeader *As #PRIM_GDCL).SortDirection = Ascending)
(#RowHeader *As #PRIM_GDCL).SortDirection := Descending
(#RowHeader *As #PRIM_GDCL).Image <= #FP_BM004
Else
(#RowHeader *As #PRIM_GDCL).SortDirection := Ascending
(#RowHeader *As #PRIM_GDCL).Image <= #FP_BM005
Endif
Else
* If not, we set it as the sort column with ascending sort direction
(#RowHeader *As #PRIM_GDCL).SortPosition := 2
(#RowHeader *As #PRIM_GDCL).SortDirection := Ascending
(#RowHeader *As #PRIM_GDCL).Image <= #FP_BM005
Endif

* Reset all other sort positions
For Each(#Column) In(#GRID_1.Columns)
* Already processed the row header that was clicked
If_Ref Com(#Column) Is_Not(*EQUAL_TO #RowHeader)
* Do not change sort order of the hidden column (stays at one)
If_Ref Com(#Column) Is_Not(*EQUAL_TO #TotalSort)

#Column.SortPosition := 0
#Column.Image <= *null

Endif

Endif
Endfor

Endroutine
End_Com