索引在FDQuery中不起作用 [英] Indexes don't work in FDQuery

查看:196
本文介绍了索引在FDQuery中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FDQuery,可将数据馈送到网格。

当用户单击列时,我希望网格在该列上排序。
因为我希望能够对多列进行排序,所以不能使用网格的自动排序选项。

I have a FDQuery that feeds data to a grid.
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.

我在做什么错了?

PS我宁愿不诉诸于构造自定义 order by 子句,但我知道这是一个选择。

P.S. I'd rather not resort to constructing a custom order by clause but I know that's an option.

推荐答案

我认为您可能缺少一个步骤,即将FDQuery的IndexName设置为所添加索引的名称。显然。设置添加的索引的Active属性是不够的。

I think you may be missing a step, namely setting the FDQuery's IndexName to the name of the added index. Apparently. setting the added index's Active property is insufficient.

对于MS Sql Server pubs数据库Authors表,以下对我来说很好用:

The following works fine for me against the MS Sql Server pubs database Authors table:

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 ;=)

这篇关于索引在FDQuery中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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