Example 40 : sort independent cell access

vcl grid sorting

Being able to access cells independent of the sorting order can be a huge advantage when developing applications. TAdvStringGrid makes this possible with 3 methods:

This method initializes the current row sequence as the default row sequence with which further row addressing can be done. Call this method before allowing the user to sort the grid.
grid.SortedRowIndex(ARow: Integer): Integer;

This method returns the original row value corresponding with the new sorted row index. If row X was at position Y before the sort was applied, calling grid.SortedRowIndex(X) will return this value Y.
grid.UnsortedRowIndex(ARow: Integer): Integer;

This method returns the new row index for the row that was originally at index ARow before the sort. If row X is at position Y after the sort was applied, calling grid.UnSortedRowIndex(Y) will return this value X.

To demonstrate a possible use of these functions, the OnGetCellColor event is written in such way that it displays the rows 12 to 29 (which hold the Audi car models) in a blue bold font. To keep these Audi car models in bold blue font irrespective of the sorting, the ARow parameter in the OnGetCellColor event is converted with the grid.SortedRowIndex method to the original index. Based on this original index, the color can now be set correct.

Delphi project & source files for downloading included in the main demos distribution for Delphi.