The ctList control can be sorted on any column. This can be done through two different methods.
However, there may be times when sorting based on character data is not sufficient (i.e. sorting on numeric values or dates). There is a method provided by the control that allows the control to be sorted on numeric data.
Sorting Non Character Data
By default, the data is sorted based on character data. However, this may not be sufficient when we need to sort integers, reals (like currency), and dates. In this case, we can set the ColumnDataType property to match the type of data that needs to be sorted. The control will automatically sort the control in the appropriate manner.
The list can also be sorted based on the data in the ListData and ListCargo properties. For instance, the example below will sort the data based on the ListData property whenever column 2 is sorted.
Private Sub ctList_PreSort( ByVal nColumn as Integer ) |
'If a value of -1 is supplied to the SortColumn or SortNewColumn property/method, the sort will be conducted based on the data in the ListData property. If a value of -2 is supplied to this property, the sort will be conducted based on the data in the ListCargo property. |
if nColumn = 2 |
ctList.SortNewColumn -1 |
End If |
End Sub |
Note : While it is not possible to hide columns in ctList, you can assign text to columns that do not exist and sort the list based on that data. For instance, you can define 3 columns but supply text for each list item for four columns. If we sort the list based on the forth column, we essentially sort the data based on a hidden column.
Sorting Multiple Columns
It is possible to sort up to three columns at a time. The SortMultiColumns method allows the programmer to specify up to three different columns to be sorted. The method can be used both inside and outside the PreSort method. For instance…
Private Sub ctList_PreSort( ByVal nColumn as Integer ) |
'The first parameter for SortMultiColumns can accept a -1 or -2 value for the line item data or cargo as part of the sort. However, the next two parameters must be supplied with a column number which is 0 or greater. |
if nColumn = 2 |
ctList.SortMultiColumns nColumn, 3, 4 |
End If |
End Sub |
- or - |
Private Sub ctList_PreSort( ByVal nColumn as Integer ) |
ctList.SortMultiColumns 1, 3, 4 |
ctList.SortList |
End Sub |