Icon Searching and Sorting DataSets

The TDataSet component provides several methods for searching and sorting the rows present in the underlying dataset, as well as properties for obtaining information about the active sort.

Sorting the Rows
To sort the rows in a dataset, use the Sort method. To specify the columns to sort, assign the desired value to the TDataColumn SortDirection property in the order that reflects the column order of the desired sort. Use the TDataSet SortCaseInsensitive property to specify that the sort should be case-insensitive, and the SortLocaleInsensitive property to specify that the sort should be locale-insensitive. The default value for both properties is False.

The following example sorts the Products dataset based upon descending list price:

begin
   with Products do
      begin
      Columns['ListPrice'].SortDirection:=sdDescending;
      Sort;
      end;
end;

Once a sort has been established, the TDataSet Sorted will return True and the dataset will automatically keep the rows sorted accordingly as rows are inserted, updated, or deleted. The TDataColumn SortIndex property can be examined to determine where a column resides in the active sort. To clear an existing sort, simply assign a value of sdNone to the SortDirection property of all sorted columns.

Searching for a Row
The TDataSet InitFind and Find methods allow you to search the rows in the dataset for a particular set of column values. The first step to executing a search is to call the InitFind method, which puts the dataset in the "Find" state, which is represented by the TDataSet State property. Once the dataset is in the "Find" state, you can assign values to the columns in the dataset and then call the Find method to execute the actual search. If there is a sort active on the dataset, then it will be used for satisfying the Find operation if the modified columns and the CaseInsensitive parameter to the Find method match the active sort. For example, the following example sorts the Products dataset by the ProductID column and then executes a case-insensitive Find operation on the ProductID column for the 'PEN-BP-12PK' product ID:

begin
   with Products do
      begin
      Columns['ProductID'].SortDirection:=sdAscending;
      SortCaseInsensitive:=True;
      Sort;
      InitFind;
      Columns['ProductID'].AsString:='PEN-BP-12PK';
      if Find(False,True) then
         Result:=True
      else
         Result:=False;
      end;
end;

To perform a search for the row with the column values that are nearest to the specified Find values, simply pass True as the first parameter to the Find method.

Information The TDataSet component determines which columns participate in the Find operation, and subsequently which columns need to match the active sort, based upon which columns have been modified since the InitFind method was called. In order to perform a nearest value search, the modified columns and the CaseInsensitive Find parameter (the second) must match the active sort.
Image