TClientDataSet[14]: 测试 FindFirst、FindNext、FindLast、FindPrior、Found


//先在窗体添加 ClientDataSet1 和两个 Button

{ 初始化测试数据 }
procedure TForm1.FormCreate(Sender: TObject);
begin
  with ClientDataSet1 do begin
    FieldDefs.Add('ID', ftInteger);
    FieldDefs.Add('Name', ftString, 6);
    FieldDefs.Add('Age', ftWord);
    CreateDataSet;
    AppendRecord([1, 'A',    11]);
    AppendRecord([2, 'AB',   22]);
    AppendRecord([3, 'ABC',  33]);
    AppendRecord([4, 'ABCD', 44]);
    AppendRecord([5, 'a',    55]);
    AppendRecord([6, 'ab',   66]);
    AppendRecord([7, 'abc',  77]);
    AppendRecord([8, 'abcd', 88]);
  end;
end;

{ FindFirst、FindNext、FindLast、FindPrior }
procedure TForm1.Button1Click(Sender: TObject);
begin
  ClientDataSet1.Filter := 'Name Like ''%c%''';
  ClientDataSet1.FilterOptions := [foCaseInsensitive];
  ClientDataSet1.Filtered := True;

  if ClientDataSet1.FindFirst then ShowMessage(ClientDataSet1['Name']); { ABC }
  if ClientDataSet1.FindNext then ShowMessage(ClientDataSet1['Name']);  { ABCD }
  if ClientDataSet1.FindLast then ShowMessage(ClientDataSet1['Name']);  { abcd }
  if ClientDataSet1.FindPrior then ShowMessage(ClientDataSet1['Name']); { abc }

  ClientDataSet1.Filtered := False;
end;

{ 遍历过滤结果 }
procedure TForm1.Button2Click(Sender: TObject);
begin
  ClientDataSet1.Filter := 'Name Like ''%c%''';
  ClientDataSet1.FilterOptions := [foCaseInsensitive];
  ClientDataSet1.Filtered := True;

  ClientDataSet1.FindFirst;
  while ClientDataSet1.Found do
  begin
    ShowMessage(ClientDataSet1['Name']); { ABC / ABCD / abc / abcd}
    ClientDataSet1.FindNext;
  end;

  ClientDataSet1.Filtered := False;
end;

 
 
 
 
 

 

 
  

你可能感兴趣的:(TClientDataSet[14]: 测试 FindFirst、FindNext、FindLast、FindPrior、Found)