Filter dbgrid with lookup fields

Asked

Viewed 605 times

2

I made a generic search form for my application, but now I realized that I can’t 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 the application I use together with 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;
  • Devexpress components do this very easily. But I believe it is not feasible to buy them just for this functionality. I’ve never had to implement this, but let’s see if anyone comes up with a more viable alternative.

  • @Arthurdeandrade really buy Devexpress not for me, it had to be very cheap. I will use a query for now, as the form is managed it will not take trouble to change. Vlw.

  • @Arthurdeandrade but in the case Devexpress has a Grid component that allows this?

  • @user21051, take a look at this article that might help you: http://ainfo.cnptia.embrapa.br/digital/bitstream/CPPSE/15593/1/PROCIRFS2005.0004.PDF It talks about using an Onfilterrecord event to do this

1 answer

0


Define DataSet.Filtered := True and add the following code to your Dataset’s Onfilterrecord:

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 set by your search button (along with Filtered = True).

Also add StrUtils at the uses to use the function AnsiContainsText.

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

  • is an application in 3 layers Datasnap expensive @Gabrielf. In this case you indicate I do a query even and never bring all the data to later filter?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.