ClientDataSet Filter

摘要:ClientDataSet Filter

用到紀錄一下

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, StdCtrls, DBClient;

type
  TForm1 = class(TForm)
    ClientDataSet1: TClientDataSet;
    Button1: TButton;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    ClientDataSet2: TClientDataSet;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    function GET_CDS_DATA(): string;
  end;

var
  Form1: TForm1;
implementation

{$R *.dfm}

function TForm1.GET_CDS_DATA(): string;
begin
   with ClientDataSet1 do begin
        FieldDefs.Add('CID', ftInteger);
        FieldDefs.Add('Name', ftString, 10);
        FieldDefs.Add('Age', ftInteger);
        CreateDataSet;
        AppendRecord([1, 'Joe',    15]);
        AppendRecord([2, 'JoeMan',   25]);
        AppendRecord([3, 'JoeSong',  35]);
        AppendRecord([4, 'JoeLine', 45]);
        AppendRecord([5, 'joe',    55]);
        AppendRecord([6, 'joeman',   65]);
        AppendRecord([7, 'joesong',  75]);
        AppendRecord([8, 'joeline', 85]);
        AppendRecord([9, 'joeCall']);
   end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
GET_CDS_DATA();
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ClientDataSet1.Filter := 'Age<=77 and Age>=33 ';
  ClientDataSet1.Filtered := True;

end;
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
begin
   s:=Edit1.Text;
 //ClientDataSet1.Filter := 'Name = ''joe''';
 ClientDataSet1.Filter := 'Name = ' + QuotedStr(s);
  ClientDataSet1.Filtered := True;
end ;

procedure TForm1.Button2Click(Sender: TObject);
begin
// ClientDataSet1.Filter := 'Name like ''%man%''';
 ClientDataSet1..Filter := 'Name LIKE '+ QuotedStr('%'+ edit.Text + '%');
  ClientDataSet1.FilterOptions := [foCaseInsensitive];
  ClientDataSet1.Filtered := True;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  ClientDataSet1.Filter := 'Age = Null';
  ClientDataSet1.Filtered := True;
end;



end.

 

 

cds_test.zip

 

 

 

 

 

參考

http://edn.embarcadero.com/article/29271

 

 

 

 

大家一起加入blogads 賺零用錢!!