3

我想实现一个可排序的 DBgrid(单击列标题时对其行进行排序)。我设法使它按升序排序,但我不能按降序排序。这是我的设计设置:

Query1.DatabaseName:='Test';
DataSetProvider1.DataSet:=Query1;
ClientDataSet1.ProviderName:=DataSetProvider1;
DataSource1.DataSet:=ClientDataSet1;
DBGrid1.DatSource:=DataSource1;

这是我的代码片段:

procedure TForm2.FormShow(Sender: TObject);
begin
  Query1.Open;
  ClientDataSet1.Data:=DataSetProvider1.Data;
  ClientDataSet1.AddIndex('objnameDESC','objname',[ixDescending]);
  ClientDataSet1.AddIndex('SUM(cd.worktime)DESC','SUM(cd.worktime)',[ixDescending]);
end;

procedure TForm2.DBGrid1TitleClick(Column: TColumn);
begin
  case Column.Index of
  0: if ClientDataSet1.IndexFieldNames='objname' then
       ClientDataSet1.IndexFieldNames:='objnameDESC'
     else
       ClientDataSet1.IndexFieldNames:='objname';
  1: if ClientDataSet1.IndexFieldNames='SUM(cd.worktime)' then
       ClientDataSet1.IndexFieldNames:='SUM(cd.worktime)DESC'
     else
       ClientDataSet1.IndexFieldNames:='SUM(cd.worktime)';
  end;
end;

当我第一次单击列标题时,排序是按升序完成的——所以到这里一切都很好。当我第二次单击时,我希望按降序排序,但我收到了以下消息:

Project ... raised Exception class EDatabaseError with message
'ClientDataSet1: Field 'objnameDESC' not found'.

关于我做错了什么的任何想法?

4

4 回答 4

6

由于您已经在使用 TClientDataSet,您可能会使用我为此目的制作的组件。创建一个实例,设置它的 Grid 属性,它会自动连接到 OnTitleClick 事件。

type
  TDBGridSorter = class(TComponent)
  strict private
    FSortColumn: TColumn;
    FGrid: TDBGrid;
    procedure CreateIndex(const FieldName: string; Descending: Boolean);
    function GetDataSet: TClientDataSet;
      function MakeIndexName(const FieldName: string; Descending: Boolean): string;
    procedure SetSortColumn(const Value: TColumn);
    procedure SortByField(const FieldName: string; out Descending: Boolean);
  private
    procedure SetGrid(const Value: TDBGrid);
  strict protected
    procedure GridTitleClick(Column: TColumn);
    property DataSet: TClientDataSet read GetDataSet;
  public
    property Grid: TDBGrid read FGrid write SetGrid;
    property SortColumn: TColumn read FSortColumn write SetSortColumn;
  end;

procedure TDBGridSorter.CreateIndex(const FieldName: string; Descending: Boolean);
var
  cds: TClientDataSet;
  indexDef: TIndexDef;
  indexName: string;
begin
  cds := DataSet;
  if cds <> nil then begin
    indexName := MakeIndexName(FieldName, Descending);
    if cds.IndexDefs.IndexOf(indexName) < 0 then begin
      indexDef := cds.IndexDefs.AddIndexDef;
      indexDef.Name := indexName;
      indexDef.Fields := FieldName;
      indexDef.CaseInsFields := FieldName;
      if Descending then
        indexDef.DescFields := FieldName;
    end;
  end;
end;

function TDBGridSorter.GetDataSet: TClientDataSet;
begin
  if (Grid <> nil) and (Grid.DataSource <> nil) and (Grid.DataSource.DataSet is TClientDataSet) then
    Result := TClientDataSet(Grid.DataSource.DataSet)
  else
    Result := nil;
end;

procedure TDBGridSorter.GridTitleClick(Column: TColumn);
begin
  SortColumn := Column;
end;

function TDBGridSorter.MakeIndexName(const FieldName: string; Descending: Boolean): string;
const
  cAscDesc: array[Boolean] of string = ('_ASC', '_DESC');
begin
  Result := FieldName +  cAscDesc[Descending];
end;

procedure TDBGridSorter.SetGrid(const Value: TDBGrid);
begin
  if FGrid <> Value then begin
    if FGrid <> nil then begin
      FGrid.OnTitleClick := nil;
      FGrid.RemoveFreeNotification(Self);
    end;
    FGrid := Value;
    if FGrid <> nil then begin
      FGrid.FreeNotification(Self);
      FGrid.OnTitleClick := GridTitleClick;
    end;
  end;
end;

procedure TDBGridSorter.SetSortColumn(const Value: TColumn);
const
  cOrder: array[Boolean] of string = ('˄', '˅');
var
  descending: Boolean;
  S: string;
begin
  if FSortColumn <> nil then begin
    S := FSortColumn.Title.Caption;
    if StartsStr(cOrder[false], S) or StartsStr(cOrder[true], S) then begin
      Delete(S, 1, 2);
      FSortColumn.Title.Caption := S;
    end;
  end;
  FSortColumn := Value;
  if FSortColumn <> nil then begin
    SortByField(FSortColumn.FieldName, descending);
    FSortColumn.Title.Caption := Format('%s %s', [cOrder[descending], FSortColumn.Title.Caption]);
  end;
end;

procedure TDBGridSorter.SortByField(const FieldName: string; out Descending:
    Boolean);
var
  cds: TClientDataSet;
  curIndex: TIndexDef;
  N: Integer;
begin
  cds := DataSet;
  if cds <> nil then begin
    descending := false;
    N := cds.IndexDefs.IndexOf(cds.IndexName);
    if N >= 0 then begin
      curIndex := cds.IndexDefs[N];
      if SameText(FieldName, curIndex.Fields) then
        descending := not (ixDescending in curIndex.Options)
    end;
    { make sure the index exists }
    CreateIndex(FieldName, descending);
    cds.IndexName := MakeIndexName(FieldName, descending);
  end;
end;
于 2012-09-24T15:08:03.477 回答
2

错误的分配

除了进行了不正确的分配之外,不可能切换回“升序”。

对于 2 列,您需要 4 个索引。

假设 'objname' 和 'SUM(cd.worktime)' 是字段。

procedure TForm2.FormShow(Sender: TObject);
....
ClientDataSet1.AddIndex('col0_asc','objname',[]);
ClientDataSet1.AddIndex('col0_desc','objname',[ixDescending]);
ClientDataSet1.AddIndex('col1_asc','SUM(cd.worktime)',[]);
ClientDataSet1.AddIndex('col1_desc','SUM(cd.worktime)',[ixDescending]);
....

使用ClientDataSet1.IndexName

procedure TForm2.DBGrid1TitleClick(Column: TColumn);
begin
  case Column.Index of
  0: if ClientDataSet1.IndexName='col0_asc' then
       ClientDataSet1.IndexName:='col0_desc'
     else
       ClientDataSet1.IndexName:='col0_asc';
  1: if ClientDataSet1.IndexName='col1_asc' then
       ClientDataSet1.IndexName:='col1_desc'
     else
       ClientDataSet1.IndexName:='col1_asc';
  end;
....

或者更短

procedure TForm2.DBGrid1TitleClick(Column: TColumn);
begin
     if ClientDataSet1.IndexName='col'+IntToStr(Column.Index)+'_asc' then
       ClientDataSet1.IndexName:='col'+IntToStr(Column.Index)+'_desc'
     else
       ClientDataSet1.IndexName:='col'+IntToStr(Column.Index)+'_asc';
....

但最好测试活动的列数 (AddIndex = done)

procedure TForm2.DBGrid1TitleClick(Column: TColumn);
begin
   if Column.Index < 2 then begin 
     if ClientDataSet1.IndexName='col'+IntToStr(Column.Index)+'_asc' then
       ClientDataSet1.IndexName:='col'+IntToStr(Column.Index)+'_desc'
     else
       ClientDataSet1.IndexName:='col'+IntToStr(Column.Index)+'_asc';
   end;
....
于 2012-09-25T03:38:44.550 回答
1

你应该设置IndexName而不是IndexFieldNamesIndexFieldNames接受字段名称并动态创建索引。

procedure TForm2.DBGrid1TitleClick(Column: TColumn);
begin
  case Column.Index of
  0: if ClientDataSet1.IndexFieldNames='objname' then
       ClientDataSet1.IndexName:='objnameDESC'
     else
       ClientDataSet1.IndexFieldNames:='objname';
  1: if ClientDataSet1.IndexFieldNames='SUM(cd.worktime)' then
       ClientDataSet1.IndexFieldNames:='SUM(cd.worktime) DESC'
     else
       ClientDataSet1.IndexFieldNames:='SUM(cd.worktime)';
  end;
end;
于 2012-09-24T19:33:32.770 回答
0

我的许多程序都需要这个,所以我编写了一个通用程序,为数据集中的每个字段构建两个索引

Procedure BuildIndices (cds: TClientDataSet);
var
 i, j: integer;
 alist: tstrings;

begin
 with cds do
  begin
   open;
   for i:= 0 to FieldCount - 1 do
   if fields[i].fieldkind <> fkCalculated then
    begin
     j:= i * 2;
     addindex ('idx' + inttostr (j), fieldlist.strings[i], [], '', '', 0);
     addindex ('idx' + inttostr (j+1), fieldlist.strings[i], [ixDescending], '', '',0);
    end;
   alist:= tstringlist.create;
   getindexnames (alist);
   alist.free;
   close;
  end;
end;

在这个阶段,我有第一个字段的索引 idx0 和 idx1,第二个字段的索引 idx2 和 idx3,等等。

然后,在显示 dbgrid 的表单中(这里的活动查询称为 qShowFees)

procedure TShowFees.DBGrid1TitleClick(Column: TColumn);
var
 n, ex: word;

begin
 n:= column.Index;
 try
  dbGrid1.columns[prevcol].title.font.color:= clNavy
 except
 end;

 dbGrid1.columns[n].title.font.color:= clRed;
 prevcol:= n;
 directions[n]:= not directions[n];

 ex:= n * 2;
 if directions[n] then inc (ex);
 with qShowFees do
  try
   disablecontrols;
   close;
   indexname:= 'idx' + inttostr (ex);
   open
  finally
   enablecontrols
  end;
end;

'Directions' 是一个布尔值的表单数组,它“记住”每列当前的排序方式(升序或降序),因此第二次单击 dbgrid 的标题栏将导致网格以与其排序相反的方式排序前。'Prevcol' 是一个表单变量,用于存储当前选择的列;这是在调用之间保存的,因此下次用户打开表单时,它的排序方式与她之前离开的方式相同。

于 2012-09-25T05:19:54.170 回答