TAdvStringGrid

Example 78 : Incremental filtering & narrow down capabilities

vcl grid filtering

From version 4.5, the filter capability has been extended with a filter condition that can be set for any column in the grid, filters that can be applied/removed and the new function grid.NarrowDown. Typically, a filter condition consists of the condition itself and the column for which to apply the filter. If it is not known in advance in what column some information is present and you want to filter on it, it is now possible to specify the entire row should be searched. To specify a filter that applies for an entire row, set Filter.Data to fcRow. Example:
with grid.Filter.Add do 
begin 
  Condition := 'sometext';
  Data := fcRow;
end;
grid.FilterActive := true;
This code will filter any row that contains in any of its columns the word "sometext".

Incrementally applying filters can be done by calling grid.ApplyFilter several times after each other. To remove the last filter or to remove all filters call grid.RemoveLastFilter or grid.RemoveAllFilters respectively. In the code snippet below, two filters are applied after each other and finally the last filter operation is removed, leaving the result of the first applied filter:
procedure TForm2.Button1Click(Sender: TObject);
begin
  with advstringgrid1.Filter.Add do
  begin
    condition := '>50';
    column := 1;
  end;
  advstringgrid1.ApplyFilter;

  with advstringgrid1.Filter.Add do
  begin
    condition := '<75';
    column := 1;
  end;
  advstringgrid1.ApplyFilter;

  advstringgrid1.RemoveLastFilter;

end;

A last new feature is the Narrow-Down capability. With this method, the grid can filter in incremental steps to find all rows with a specific word or all rows that have a specific word in one column. In the demo, the edit control's OnChange method event handler just calls grid.NarrowDown(searchvalue). This causes that the grid will always show all rows containing the word in the edit control as the user types the search specification in the edit control. The checkbox controls whether the search for a word in a row is done for the full row or restricted to one column only (column 1 in this case):

procedure TForm2.Edit1Change(Sender: TObject);
begin
  if checkbox1.checked then
    advstringgrid1.NarrowDown(Edit1.Text, 1)
  else
    advstringgrid1.NarrowDown(Edit1.Text);
end;



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