程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> TClientDataSet[13]: 過濾

TClientDataSet[13]: 過濾

編輯:Delphi

 方法有:

  1、Filter、Filtered

  2、OnFilterRecord

  3、SetRange、ApplyRange 或 SetRangeStart、SetRangeEnd、ApplyRange

  (Range 相關還有: CancelRange、EditRangeStart、EditRangeEnd、KeyExclusive、Ranged)

  下面三個例子測試前都要: 先在窗體上放置 ClIEntDataSet1、DataSource1、DBGrid1 並關聯.

  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; 



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved