Frequently Asked Component Specific Questions

Options

Display all FAQ items

Search FAQ items:


Displaying items 1 to 1 of 1, page 1 of 1

<< previous next >>

TAdvStringGrid
Auto filtering in a grid with dynamically formatted cell values based on real cell values

Suppose a grid filled with numbers loaded from a database for example but these numbers are dynamically formatted via the OnGetDisplText event. If we want to perform auto filtering via a filter dropdown, the filtering typically needs to be performed on the real cell data and not the formatted cell text. In this case, the formatting that is done is to add a currency suffix €. In the filter condition we also want to display the currency suffix but we still want the filtering to be applied on the real cell value with real value filter conditions.

The grid is filled with randome numbers between 0 and 100 and the formatting is applied via OnGetDisplText to append the currency suffix €:

procedure TForm1.FormCreate(Sender: TObject);
begin
  advstringgrid1.RandomFill(false);
  advstringgrid1.FilterDropDownCheck := true;
end;

procedure TForm1.AdvStringGrid1GetDisplText(Sender: TObject; ACol,
  ARow: Integer; var Value: string);
begin
  if (arow > 0) and (acol > 0) then
    Value := Value +''€'';
end;

The filter dropdown is filled with 3 conditions and these conditions include the currency suffix:

procedure TForm1.AdvStringGrid1GetColumnFilter(Sender: TObject; Column: Integer;
  Filter: TStrings);
begin
  Filter.Add(''>10€ & <20€'');
  Filter.Add(''>20€ & <50€'');
  Filter.Add(''>50€'');
end;

First thing that needs to be done is set the real filter condition based on numbers only from the OnFilterCheck event:

procedure TForm1.AdvStringGrid1FilterCheck(Sender: TObject; Column,
  ItemIndex: Integer; FriendlyName: string; var FilterCondition: string;
  CheckList: TCheckListBox);
begin
  //
  caption := inttostr(advstringgrid1.Filter.Count);
  case ItemIndex of
  0: FilterCondition := ''>10 & <20'';
  1: FilterCondition := ''>20 & <50'';
  2: FilterCondition := ''>50'';
  end;
end;

Then we need to instruct for the filter condition to take the real cell value and not the formatted value in account:

This can be done in two ways in the OnBeforeFilter event that is triggered when the filter conditions are all built-up and just before these will be applied. Either set the filter instructions to use the real grid value or to specify the suffix:

Method 1:

procedure TForm1.AdvStringGrid1BeforeFilter(Sender: TObject);
var
  i:integer;
begin
  for i := 0 to advstringgrid1.Filter.Count - 1 do
    advstringgrid1.Filter.Items[i].Data := fcNormal;
end;


Method 2:

procedure TForm1.AdvStringGrid1BeforeFilter(Sender: TObject);
var
  i:integer;
begin
  for i := 0 to advstringgrid1.Filter.Count - 1 do
    advstringgrid1.Filter.Items[i].Suffix := ''€'';
end;