德尔福TEdit与Access过滤Tstringgrid [英] Delphi TEdit to filter Tstringgrid with Access

查看:209
本文介绍了德尔福TEdit与Access过滤Tstringgrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Delphi 10与Firemonkey,我有点新。我有一个TStringGrid,我用LiveBindings绑定到一个访问数据库。我需要的是过滤此表或TStringGrid TEdit的文本,当我按下按钮或任何键进入,并显示在同一个TstringGrid的结果中。就像自定义搜索/过滤器框一样。



我还没有任何代码。但我认为这将是一个查询。

procedure TForm3.Edit2Typing(Sender:TObject);
begin
adoquery1.Close;
adoquery1.SQL.Text:='select * from instrutor其中nome就像%'+ edit2.text +'%';
adoquery1.Open;
end;



我已经试过了,但是因为我已经有了adobable和stringgrid的livebinding,所以我不这样做应链接此Tedit

解决方案下面的示例是使用TAdoQuery和TStringGrid之间的活动绑定的最小应用程序。为了方便起见,我已经做了一个VCL应用程序而不是FMX,但是如何在
实时绑定的AdoQuery上进行过滤并没有什么区别。



它使用2个TEdits来指定要匹配的过滤器值和要过滤的字段
的名称(在实践中,最好是使用可用的字段名填充列表框) 。
$ b

主要的工作是在 UpdateFilter 过程中完成的。



使用实时绑定的事实并不会影响如何将过滤器应用于数据集。但是,与StringGrid的活动绑定比传统的(VCL)TDBGrid要慢得多。要避免的一个重要的事情是,数据集有大量的字段,并且每个字段有一个stringgrid列,因为它可以使应用程序非常缓慢地响应过滤条件的变化。通过将字符串网格的ColCount设置为适当的低值,可以减轻这种影响的一种方法是将字符串网格列的数量限制为更低的数字。另一种方法是为数据集定义持久字段,但只能创建一些字段。



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

代码:

  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;
过程SetFilterFieldName(const Value:String);
过程SetFilterValue(const Value:String);
过程UpdateFilter;
public
属性FilterFieldName:字符串读取FFilterFieldName写入SetFilterFieldName;
属性FilterValue:字符串读取FFilterValue写入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);
如果FilterFieldName<>则开始
。然后开始
FFilterFieldName:= Value;
UpdateFilter;
end;
end;

procedure TForm1.SetFilterValue(const Value:String);
如果FilterValue<>则开始
。然后开始
FFilterValue:= Value;
UpdateFilter;
end;
end;

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

//下一个语句检查FilterFieldName
//是否匹配数据集中的一个字段,如果不匹配则退出。由于
// FilterFieldName值来自一个编辑框,所以如果AdoQuery1.FieldByName(FilterFieldName)= Nil然后
exit,用户在
中输入它将会不完整。
FilterValue<> ''然后开始
Expr:= FilterFieldName +'like'+ QuotedStr('%'+ FilterValue +'%');
AdoQuery1.Filter:= Expr;
AdoQuery1.Filtered:= True;
end;
end;


I'm using Delphi 10 with Firemonkey, i'm a bit new to it. I have a TStringGrid that i bound with LiveBindings to a access database. What I need is to filter this table or the TStringGrid with the text of a TEdit when I press a button or when any key is enter, and the show it in the results in the same TstringGrid. Just like a custom search/filter box.

I don't have any code of this yet. But i think it would be like a query.
procedure TForm3.Edit2Typing(Sender: TObject); begin adoquery1.Close; adoquery1.SQL.Text:='select * from instrutor where nome like " %' + edit2.text + '%"'; adoquery1.Open; end;
I've tried this but since I already have a livebinding with the adotable and the stringgrid, i don't the way i should link this Tedit

解决方案

The example below is a minimal application using live binding between a TAdoQuery and a TStringGrid. I've done it for convenience as a VCL application rather than an FMX one, but that doesn't make any difference to how to do filtering on a live-bound AdoQuery.

It uses 2 TEdits to specify the filter value to match and the name of the field to filter on (in practice it would be better to do something like populate a listbox with the available field names).

The main "work" is done in the UpdateFilter procedure.

The fact that it uses live binding doesn't make any difference to how to apply the filter to the dataset. However, live binding to a StringGrid is significantly slower than a traditional (VCL) TDBGrid. An important thing to avoid is the situation where the dataset has a large number of fields and there is one stringgrid column for each field because it can make the app very slow to respond to changes in the filter criteria. A way to mitigate the effect of this is simply to restrict the number of stringgrid columns to a much lower number, by setting the ColCount of the stringgrid to a suitably low value. An alternative is to define persistent fields for the dataset, but only create a few of them.

In the code below, I've used the OnChange events of the TEdits to update the FilterFieldName and FilterValue fields, but obviously you could have a separate button to click to call the UpdateFilter procedure.

Code:

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;

这篇关于德尔福TEdit与Access过滤Tstringgrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆