方法有:
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;
补充: _ 在使用 like 时课代表单个字符.