Delphi TEdit 以使用 Access 过滤 Tstringgrid

Delphi TEdit to filter Tstringgrid with Access

我正在使用 Delphi 10 和 Firemonkey,我对它有点陌生。我有一个与 LiveBindings 绑定到访问数据库的 TStringGrid。我需要的是在按下按钮或输入任何键时使用 TEdit 的文本过滤此 table 或 TStringGrid,并将其显示在同一个 TstringGrid 的结果中。就像自定义 search/filter 框一样。

我还没有这方面的任何代码。但我认为这就像一个查询。
procedure TForm3.Edit2Typing(Sender: TObject); begin adoquery1.Close; adoquery1.SQL.Text:='select * from instrutor where nome like " %' + edit2.text + '%"'; adoquery1.Open; end;
我已经试过了,但是因为我已经有了 adotable 和 stringgrid 的实时绑定,所以我不应该 link 这个 Tedit

下面的示例是在 TAdoQuery 和 TStringGrid 之间使用实时绑定的最小应用程序。为了方便起见,我将其作为 VCL 应用程序而不是 FMX 应用程序来完成,但这对如何在 实时绑定 AdoQuery。

它使用2个TEdits来指定要匹配的过滤器值和字段的名称 过滤(实际上最好做一些事情,比如用可用的字段名称填充列表框)。

主要 "work" 在 UpdateFilter 过程中完成。

它使用实时绑定这一事实对如何将过滤器应用于数据集没有任何影响。但是,实时绑定到 StringGrid 比传统 (VCL) TDBGrid 慢得多。要避免的一件重要事情是数据集有大量字段并且每个字段都有一个 stringgrid 列的情况,因为它会使应用程序响应过滤条件的变化非常慢。减轻这种影响的一种方法是通过将 stringgrid 的 ColCount 设置为适当的低值,将 stringgrid 列的数量限制为一个非常低的数量。另一种方法是为数据集定义持久字段,但只创建其中的几个字段。

在下面的代码中,我使用了 TEdits 的 OnChange 事件来更新 FilterFieldName 和 FilterValue 字段,但显然您可以通过单击一个单独的按钮来调用 UpdateFilter 过程。

代码:

TForm1 = class(TForm)
  ADOConnection1: TADOConnection;
  ADOQuery1: TADOQuery;
  StringGrid1: TStringGrid;
  BindingsList1: TBindingsList;
  DataSource1: TDataSource;
  LinkGridToDataSource1: TLinkGridToDataSource;
  BindSourceDB1: TBindSourceDB;
  edFilterFieldName: TEdit;
  edFilterValue: TEdit;
  procedure FormCreate(Sender: TObject);
  procedure edFilterFieldNameChange(Sender: TObject);
  procedure edFilterValueChange(Sender: TObject);
private
  FFilterFieldName : String;
  FFilterValue : String;
  procedure SetFilterFieldName(const Value: String);
  procedure SetFilterValue(const Value: String);
  procedure UpdateFilter;
public
  property FilterFieldName : String read FFilterFieldName write SetFilterFieldName;
  property FilterValue : String read FFilterValue write SetFilterValue;
end;

[...]

procedure TForm1.FormCreate(Sender: TObject);
begin
  FilterFieldName := edFilterFieldName.Text;
  FilterValue := edFilterValue.Text;
end;

procedure TForm1.edFilterFieldNameChange(Sender: TObject);
begin
  FilterFieldName := edFilterFieldName.Text;
end;

procedure TForm1.edFilterValueChange(Sender: TObject);
begin
  FilterValue := edFilterValue.Text;
end;

procedure TForm1.SetFilterFieldName(const Value: String);
begin
  if FilterFieldName <> Value then begin
    FFilterFieldName := Value;
    UpdateFilter;
  end;
end;

procedure TForm1.SetFilterValue(const Value: String);
begin
  if FilterValue <> Value then begin
    FFilterValue := Value;
    UpdateFilter;
  end;
end;

procedure TForm1.UpdateFilter;
var
  Expr : String;
begin
  AdoQuery1.Filtered := False;

  //  The next statement checks whether the FilterFieldName
  //  matches a field in the dataset and exits if not.  Since the
  //  FilterFieldName value comes from an edit box, it will be incomplete while the user is typing it in
  if AdoQuery1.FieldByName(FilterFieldName) = Nil then
    exit;
  if FilterValue <> '' then begin
    Expr := FilterFieldName + ' like ' + QuotedStr('%' + FilterValue + '%');
    AdoQuery1.Filter := Expr;
    AdoQuery1.Filtered := True;
  end;
end;