Filter dbgrid with lookup fields

2

I did a generic search form for my application, but now I realized that I can not filter the dataset using lookup fields, is there an alternative to not using a query? A component that can do this free or commercial? Below is the procedure that I use next to a combobox and an edit.

procedure Pesquisa(DS : TDataSource; Field : string; Value : string);
begin
  DS.DataSet.Filter := 'Upper(' + Field + ') like ' + QuotedStr('%' + UpperCase(Value) + '%');
  DS.DataSet.Filtered := True;
end;
    
asked by anonymous 01.02.2015 / 03:41

1 answer

0

Set DataSet.Filtered := True and add the following code to the OnFilterRecord of your DataSet:

procedure TForm1.DataSetFilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin
  Accept := AnsiContainsText(DataSet.FieldByName(Field).AsString, Value);
end;

Field and Value must be variables in your form that are defined by your search button (along with Filtered = True ).

Also add StrUtils to uses to be able to use the AnsiContainsText function.

PS: Unless your application is local, with a small DB, you should not use this. I, by habit, would not even use that case. I would do a dynamic query myself. This filter operates locally (as well as the Filter property) and if you're on a network, that means you're traversing the entire table to then filter locally (and certainly not indexes, optimizations, and server computing power). In my experience, the biggest performance bottleneck is in network traffic, so this has huge potential to make a VERY slow application.

    
08.06.2015 / 17:22