秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
在lazarus使用memdataset时,如果filter按下面的方法是得不到想要结果:
  MEMDataSet1.Filtered:=false;
  MEMDataSet1.Filter:=' taskname Like '+quotedstr('%'+Edit4.Text+'%');  
  MEMDataSet1.Filtered:=true;

最后在官网找到解决办法:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, memds, db, Forms, DBGrids, StdCtrls;

type

{ TForm1 }

  TForm1 = class(TForm)
     dataSource: TDataSource;
     dbGrid: TDBGrid;
     MemDataset1: TMemDataset;
     procedure FilterRecordHandler(DataSet: TDataSet; var Accept: Boolean);
     procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
  with MemDataset1 do
  begin
    FieldDefs.Add( 'id', ftInteger );
    FieldDefs.Add( 'Nombre', ftString, 255);
    OnFilterRecord:=@FilterRecordHandler;
    FilterOptions:=[foCaseInsensitive]; // change this to suit your requirements
    Active:=True;

    for i:= 0 to 200 do
    begin
      Append;
      FieldByName('id').AsInteger:= i;
      FieldByName('nombre').AsString:= 'nombre_' + IntToStr( i );
      Post;
    end;

    Filter:='11';
    Filtered:=True;
    Close; // force a rescan of table data with filter in place
    Open;
  end;
end;

procedure TForm1.FilterRecordHandler(DataSet: TDataSet; var Accept: Boolean);
begin
  Accept:=Pos(DataSet.Filter, DataSet.FieldByName('Nombre').AsString) > 0;
end;

end.

 

过滤前:

过滤后:

 

posted on 2023-05-10 15:24  秋·风  阅读(193)  评论(0编辑  收藏  举报