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.
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.
– adamasan
@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.
– Artur_Indio
@Arthurdeandrade but in the case Devexpress has a Grid component that allows this?
– Artur_Indio
@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
– Giovani