Example 80 : Using the Auto filter capability

vcl grid filtering

In sample application 77, the filter dropdown capability was demonstrated. By using the OnGetColumnFilter event, the conditions that appear in the filter dropdown from the column header can be added. When grid.AutoFilterUpdate is set to true, the filter is immediately automatically applied when a condition is selected from the dropdown.
By setting a single property: grid.FilterDropDownAuto to true, the filter dropdown is automatically filled with an alphabetically sorted list of all unique values for a given column and the filter dropdown button appears for each column. In addition to all unique column values, the first entry in the filter dropdown can be used to reset the filter. The value that appears in the dropdown list to reset the filter is set with grid.FilterDropDownClear: string and defaults to '(All)'. Note that when grid.FilterDropDownClear is an empty string, no item to reset the filter will be added to the dropdown.
Setting grid.FilterDropDownAuto to true means that every column automatically gets the auto filter capability. However, the filter can still be customized or removed for one or more columns. In this sample, the auto filter capability is used for column 1 and for column 3 a custom filter is choosen. For other columns, the filter capability is turned off. Turning off the filter for a column can be done by using the OnGetColumnFilter event and clearing the filter list. In this case, for column 3, the filter list is cleared and filled with a custom filter:
procedure TForm2.AdvStringGrid1GetColumnFilter(Sender: TObject; Column: Integer;
  Filter: TStrings);
  if Column <> 1 then

  if Column = 3 then
    Filter.Add('>2000 & <3000');

In addition, for this sample, the filter condition is also shown in the column header cell upon selection. This is done with the code:
procedure TForm2.AdvStringGrid1FilterSelect(Sender: TObject; Column,
  ItemIndex: Integer; FriendlyName: string; var FilterCondition: string);
  AdvStringGrid1.Cells[Column,0] := FilterCondition;
As the filter condition can be quite large and as such not fit in the column header cell, the filter condition is shown in a hint for the column header cell by setting:
 advstringgrid1.ShowHint := true;
 advstringgrid1.HintShowLargeText := true;

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