Recent

Author Topic: Filter a Memdataset  (Read 2291 times)

cpalx

  • Hero Member
  • *****
  • Posts: 647
Filter a Memdataset
« on: May 04, 2015, 08:15:36 pm »
The below code works, but filter  all which has '11'  ( 211, 311 ) , what about if i want to filter just with the value 11



Code: [Select]
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.

howardpc

  • Hero Member
  • *****
  • Posts: 3580
Re: Filter a Memdataset
« Reply #1 on: May 04, 2015, 08:41:08 pm »
You would need to alter the filter handler, perhaps to the following

Code: [Select]
procedure TForm1.FilterRecordHandler(DataSet: TDataSet; var Accept: Boolean);
begin
  Accept:=(Length(DataSet.FieldByName('Nombre').AsString) = 9) and
          (Pos(DataSet.Filter, DataSet.FieldByName('Nombre').AsString) > 0);
end;

 

TinyPortal © 2005-2018