Good @Luizvichiatto, as I promised
in the comment of the previous reply I made an example
that works perfectly.
Step 1: put in the Form a Datasource, Dbgrid, Edit and a Dataset in my case I used Clientdataset, but it could be anyone else that supports the Filter property.
Step 2: Configure events as below
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBClient, Grids, DBGrids;
type
TForm1 = class(TForm)
cdsBusca: TClientDataSet;
EditBusca: TEdit;
Label1: TLabel;
cdsBuscaNome: TStringField;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
procedure FormCreate(Sender: TObject);
procedure EditBuscaChange(Sender: TObject);
procedure cdsBuscaFilterRecord(DataSet: TDataSet; var Accept: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var i: integer;
begin
cdsBusca.CreateDataSet;
cdsBusca.Append;
cdsBuscaNome.Value := 'Monica';
cdsBusca.Append;
cdsBuscaNome.Value := 'Eduardo';
cdsBusca.Append;
cdsBuscaNome.Value := 'Renato Russo';
cdsBusca.Post;
end;
procedure TForm1.EditBuscaChange(Sender: TObject);
begin
cdsBusca.Filtered := False;
cdsBusca.Filter := '';
cdsBusca.Filtered := True;
end;
procedure TForm1.cdsBuscaFilterRecord(DataSet: TDataSet;
var Accept: Boolean);
begin
if EditBusca.Text = '' then
begin
cdsBusca.Filtered := False;
Exit;
end;
Accept := Pos(EditBusca.Text, cdsBuscaNome.AsString) > 0;
end;
end.
Here I added the name Edmilson, just to filter out everything that contained the letter
"And"
Filterrecord is only executed when there is something in the property
Filter
and a different record becomes active. If you need to re-read the records, change the Tquery SQL.– EMBarbosa
Then it is necessary to put a filter. And if the filter is already in the query, but need at the time of display on the grid, in case a search inside what is listed in Gridview, as for example the name. So it is not necessary to do OPEN and CLOSE in the query every time.
– Luiz Vichiatto
Tquery is not designed for this type of behavior. If you want to filter the content that has already been searched and shown in Dataset, you will have to use another component that allows this type of manipulation, such as Tclientdataset.
– EMBarbosa