TClientDataSet[13]:フィルタリング
6708 ワード
方法は次のとおりです.
1、Filter、Filtered
2、OnFilterRecord
3、SetRange、ApplyRangeまたはSetRangeStart、SetRangeEnd、ApplyRange
(Range関連は、CancelRange、EditRangeStart、EditRangeEnd、KeyExclusive、Ranged)
次の3つの例のテストの前に、ClientDataSet 1、DataSource 1、DBGrid 1をフォームに配置して関連付ける.
Filterテスト:
SetRangeなどのテスト:
OnFilterRecordテスト:
補足:likeを使用するとき、授業は単一の文字を表します.
1、Filter、Filtered
2、OnFilterRecord
3、SetRange、ApplyRangeまたはSetRangeStart、SetRangeEnd、ApplyRange
(Range関連は、CancelRange、EditRangeStart、EditRangeEnd、KeyExclusive、Ranged)
次の3つの例のテストの前に、ClientDataSet 1、DataSource 1、DBGrid 1をフォームに配置して関連付ける.
Filterテスト:
{ }
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]);
AppendRecord([9, 'abcde']);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Name = ' + QuotedStr('ab');
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Name = ''ab''';
ClientDataSet1.FilterOptions := [foCaseInsensitive];
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Name = ''a*''';
// ClientDataSet1.FilterOptions := [foNoPartialCompare]; { * }
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Name Like ''ab%''';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Name Like ''%c%''';
ClientDataSet1.FilterOptions := [foCaseInsensitive];
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'ID>2 and Age<77';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'not(ID>2 and Age<77)';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'ID<=1 or Age>=88';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'ID<>1 and ID<>3 and ID<>5 and ID<>7';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Age - ID = 20';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button11Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Age + ID > 60';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button12Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Age * 2 = 22';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button13Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Age / 2 = 22';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button14Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Age<55 and Name Like ''AB%''';
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button15Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'Age = Null'; { Null }
ClientDataSet1.Filtered := True;
end;
procedure TForm1.Button16Click(Sender: TObject);
begin
ClientDataSet1.Filter := 'ID>6 and Age<>null';
ClientDataSet1.Filtered := True;
end;
SetRangeなどのテスト:
{ }
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;
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientDataSet1.IndexFieldNames := 'ID';
ClientDataSet1.SetRangeStart;
ClientDataSet1.FieldValues['ID'] := 3;
// ClientDataSet1.KeyExclusive := True; { ; False, }
ClientDataSet1.SetRangeEnd;
ClientDataSet1.FieldValues['ID'] := 7;
// ClientDataSet1.KeyExclusive := True; { }
ClientDataSet1.ApplyRange;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ClientDataSet1.IndexFieldNames := 'ID';
ClientDataSet1.SetRange([2], [5]);
ClientDataSet1.ApplyRange;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClientDataSet1.IndexFieldNames := 'Age; Name';
ClientDataSet1.SetRange([33, 'ABC'], [66, 'abc']);
ClientDataSet1.ApplyRange;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ClientDataSet1.IndexFieldNames := 'Age; Name';
ClientDataSet1.SetRange([33, 'ABC'], [66, 'abc']);
ClientDataSet1.EditRangeStart;
ClientDataSet1.KeyExclusive := True;
ClientDataSet1.EditRangeEnd;
ClientDataSet1.KeyExclusive := True;
ClientDataSet1.ApplyRange;
end;
OnFilterRecordテスト:
{ }
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;
ClientDataSet1.Filtered := True; { OnFilterRecord }
end;
procedure TForm1.ClientDataSet1FilterRecord(DataSet: TDataSet; var Accept: Boolean);
begin
if DataSet.FieldValues['ID'] mod 2 = 0 then
Accept := True
else
Accept := False;
end;
補足:likeを使用するとき、授業は単一の文字を表します.