Example 80 : Using the Auto filter capabilityIn 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); begin if Column <> 1 then Filter.Clear; if Column = 3 then begin Filter.Add('(All)'); Filter.Add('<2000'); Filter.Add('>2000 & <3000'); Filter.Add('>3000'); end; end;
procedure TForm2.AdvStringGrid1FilterSelect(Sender: TObject; Column, ItemIndex: Integer; FriendlyName: string; var FilterCondition: string); begin AdvStringGrid1.Cells[Column,0] := FilterCondition; end;
advstringgrid1.ShowHint := true; advstringgrid1.HintShowLargeText := true;
Delphi project & source files for downloading included in the main demos distribution for Delphi