【问题标题】:Filter Query/DataSource records onto cxgrid将查询/数据源记录过滤到 cxgrid
【发布时间】:2015-03-06 16:28:25
【问题描述】:

使用 Delphi XE2。

构建一个通过查询/数据源连接到数据库的软件包。

我想为表中的记录实现过滤器选项,因此单击按钮后,cxgrid 将显示与过滤器选择匹配的记录。

我不知道该怎么做。任何帮助将不胜感激。

到目前为止有这个,但如果这接近我想要达到的目标,我真的不知道。

procedure TFilter.btnClick(Sender: TObject);
begin
with aQry do
begin
  SQL.Clear;
  Close;
  SQL.Text := 'select * from TABLE where record_name like'+QuotedStr(name.Text+'%');
  SQL.Text := 'and record_type like '+QuotedStr(type.Text+'%');
  SQL.Text := 'and record_type2 like '+QuotedStr(type2.Text+'%');
  SQL.Text := 'and record_type3 like '+QuotedStr(type3.Text+'%');
  SQL.Text := 'and record_type4 like '+QuotedStr(type4.Text+'%');
  Open;
end;
end;

【问题讨论】:

  • 首先 - 在查询中使用参数。
  • 其次 - 你会发现 TClientDataSet 可以很好地完成你想要的工作 - 查看 TClientDataSetFilter 属性的帮助。

标签: delphi delphi-xe2


【解决方案1】:

在您的代码中,您正在构建一个无效的 SQL 语句,因为每次设置 Text 属性时都会覆盖 SQL 的内容,因此您必须使用 Add 方法来构建 SQL 语句。您还必须考虑使用参数。

尝试下一个示例,该示例根据在过滤器中输入的值构建并运行参数化 SQL 语句(也许您需要修改源代码才能运行)。

  AQry.Close;
  AQry.SQL.Clear;
  AQry.SQL.Add('select * from TABLE where 1=1');
  if name.Text<>'' then
   AQry.SQL.Add('and record_name like :record_name');
  if Edittype.Text<>'' then
   AQry.SQL.Add('and record_type like :record_type');
  if type2.Text<>'' then
   AQry.SQL.Add('and record_type2 like :record_type2');
  if type3.Text<>'' then
   AQry.SQL.Add('and record_type3 like :record_type3');
  if type4.Text<>'' then
   AQry.SQL.Add('and record_type4 like :record_type4');

  if name.Text<>'' then
    Aqry.Parameters.ParamByName('record_name').Value :=  name.Text+ '%';
  if Edittype.Text<>'' then
    Aqry.Parameters.ParamByName('record_type').Value :=  Edittype.Text+ '%';
  if type2.Text<>'' then
    Aqry.Parameters.ParamByName('record_type2').Value :=  type2.Text+ '%';
  if type3.Text<>'' then
    Aqry.Parameters.ParamByName('record_type3').Value :=  type3.Text+ '%';
  if type4.Text<>'' then
    Aqry.Parameters.ParamByName('record_type4').Value :=  type4.Text+ '%';

  AQry.Open;

【讨论】:

  • 嗨 RRUZ,这确实有效.....它可以让我过滤不同的记录,但它只允许我一次做一个,当我选择多个过滤器时它不喜欢它。得到以下错误.....'语法错误或访问冲突'。
  • 您的代码有问题。如果此答案对您有用,请接受答案并提出一个新问题,发布导致问题的代码(请发布真实代码)。
【解决方案2】:

您可以使用查询的过滤器属性做您想做的事。首先,执行查询,获取所有可以显示的记录。

aQry.SQL.Text := 'select * from TABLE'; 
aQry.Open;

然后当定义过滤器的值时,定义并激活过滤器

aQry.Filtered := false;
aQry.Filter := 'record_name like'+QuotedStr(name.Text+'%') '+
  'and record_type like '+QuotedStr(type.Text+'%') '+
  'and record_type2 like '+QuotedStr(type2.Text+'%') '+
  'and record_type3 like '+QuotedStr(type3.Text+'%') '+
  'and record_type4 like '+QuotedStr(type4.Text+'%');
aQry.Filtered := true;

如果任何文本值发生更改,您将不得不再次执行上述操作。

这种方法在程序的内存中进行过滤,并且不会刷新数据库中的数据。如果您希望数据库中的数据在过滤器更改之间发生变化,RRUZ 的方法可能更适合您。

查询过滤器的替代方法是 cxGrid.TableView.DataSource.Filter。在代码中定义它比查询的过滤器要复杂一些。但是如果你让 TableView 的 Navigator 和 Navigator.Filter 可见并启用,用户可以很容易地定义它而无需任何代码。

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 2018-08-17
    相关资源
    最近更新 更多