简体   繁体   English

索引在FDQuery中不起作用

[英]Indexes don't work in FDQuery

I have a FDQuery that feeds data to a grid. 我有一个FDQuery,可以将数据提供给网格。
When the user clicks on a column I want the grid to order on that column. 当用户点击列时,我希望网格在该列上排序。 Because I want to be able to sort on multiple columns, I cannot use the autosort option of the grid. 因为我希望能够对多列进行排序,所以我无法使用网格的自动排序选项。

I tried the following code in my proof of concept. 我在我的概念证明中尝试了以下代码。 However it does not work. 但它不起作用。

procedure TForm31.JvDBGrid1TitleBtnClick(Sender: TObject; ACol: Integer;
  Field: TField);
const
  sDesc = 1;
  sASC = 2;
  sNone = 0;
var
  i: integer;
  SortClause: string;
  AField: TField;
  AIndex: TFDIndex;
begin
  case Field.Tag of
    sDesc: Field.Tag:= sASC;
    sASC: Field.Tag:= sNone;
    sNone: Field.Tag:= sDesc;
  end;
  SortClause:= '';
  FDQuery1.Indexes.BeginUpdate;
  try
    FDQuery1.Indexes.Clear;
    for i:= 0 to JvDBGrid1.Columns.Count - 1 do begin
      AField:= JvDBGrid1.Columns[i].Field;
      if AField.Tag <> sNone then begin
        AIndex:= FDQuery1.Indexes.Add;
        AIndex.Name:= AField.FieldName;
        AIndex.Fields:= AField.FieldName;
        //AIndex.Options:= [soNoCase, soNullFirst, soDescNullLast, soDescending, soUnique, soPrimary, soNoSymbols]
        case AField.Tag of
          sDESC: AIndex.Options:= [soDescNullLast];
          sASC: AIndex.Options:= [];
        end;
        AIndex.Active:= true;
      end;
    end;
  finally
    FDQuery1.Indexes.EndUpdate;
    FDQuery1.Refresh;
  end;
end;

It does not matter whether the Query already has an order by clause or not. Query是否已经有order by子句并不重要。

What am I doing wrong? 我究竟做错了什么?

PS I'd rather not resort to constructing a custom order by clause but I know that's an option. PS我宁愿不依赖于构建自定义的order by子句,但我知道这是一个选项。

I think you may be missing a step, namely setting the FDQuery's IndexName to the name of the added index. 我想你可能会错过一个步骤,即将FDQuery的IndexName设置为添加的索引的名称。 Apparently. 显然。 setting the added index's Active property is insufficient. 设置添加的索引的Active属性不足。

The following works fine for me against the MS Sql Server pubs database Authors table: 对于MS Sql Server pubs数据库Authors表,以下工作正常。

procedure TForm1.AddFDIndex;
var
  AIndex : TFDIndex;
begin
  AIndex := FDQuery1.Indexes.Add;
  AIndex.Name := 'ByCityThenlname';
  AIndex.Fields := 'city;au_lname';
  AIndex.Active := True;
  FDQuery1.IndexName := AIndex.Name;
end;

Btw, I'm not sure what your code is supposed to do if more than one column is tagged to be included in the index, but I'll leave that to you ;=) 顺便说一下,如果标记了多个列被包含在索引中,我不确定你的代码应该做什么,但我会把它留给你; =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM