Jump to content

[Solved] Grid ColumnSort with FireDac FDQuery


Recommended Posts

Hello Guys,

 

I'm testing uniGUI Trial version,

 

I would like to know how can I sort each columns of a Desktop UniDbGrid.

 

I tried to follow GridColumnSort demo that is ClientDataSet, but I'm using FireDac.

 

So at MainForm I did:

 


//OnColumnSort event
procedure TMainForm.UniDBGrid1ColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  UniMainModule.SortColumn(Column.FieldName, Direction);
end;
 
 
And at MainModule:
uses DB, DBClient;
 

procedure TUniMainModule.SortColumn(const FieldName: string; Dir: Boolean);
begin
  if Dir then
    Main.MainForm.FDQuery1.IndexName := FieldName+'_ASC'
  else
    Main.MainForm.FDQuery1.IndexName := FieldName+'_DSC';
end;
 
 

procedure TUniMainModule.UniGUIMainModuleCreate(Sender: TObject);
var
  I: Integer;
  IndexnameAsc : string;
  IndexnameDes : string;
begin
  with Main.MainForm.FDQuery1 do
  begin
    for i := 0 to FieldCount-1 do
    begin
      IndexnameAsc := Fields.FieldName+'_ASC';
      IndexnameDes := Fields.FieldName+'_DSC';
      IndexDefs.Add(IndexnameAsc, Fields.FieldName, []);
      IndexDefs.Add(IndexnameDes, Fields.FieldName, [ixDescending]);
    end;
  end;
end;
 
 

But when I click at column title nothing happens
 
What I miss?
 
Tks

 

Link to post
Share on other sites
procedure TFrameModelo.UniDBGrid1ColumnSort(Column: TUniDBGridColumn; Direction: Boolean);
begin
  if Direction then
    TFdquery(DsPes.DataSet).IndexName := Column.FieldName+'_index_asc'
  else
    TFdquery(DsPes.DataSet).IndexName := Column.FieldName+'_index_des';
end;

procedure TFrameModelo.UniFrameCreate(Sender: TObject);
var
  I: Integer;
  IndexnameAsc : string;
  IndexnameDes : string;
begin
  if Assigned(DsPes.DataSet) then
  begin
//    TFDQuery(DsPes.DataSet).IndexDefs.Clear;
    for I := 0 to TFDQuery(DsPes.DataSet).FieldCount-1 do
    begin
      IndexnameAsc := TFDQuery(DsPes.DataSet).Fields[I].FieldName+'_index_asc';
      IndexnameDes := TFDQuery(DsPes.DataSet).Fields[I].FieldName+'_index_des';

      if TFDQuery(DsPes.DataSet).Indexes.FindIndex(IndexnameAsc) = nil then
      begin
        with TFDQuery(DsPes.DataSet).Indexes.Add do
        begin
          Name    := IndexnameAsc;
          Fields  := TFDQuery(DsPes.DataSet).Fields[I].FieldName;
          Active  := True;
        end;
      end;

      if TFDQuery(DsPes.DataSet).Indexes.FindIndex(IndexnameDes) = nil then
      begin
        with TFDQuery(DsPes.DataSet).Indexes.Add do
        begin
          Name        := IndexnameDes;
          Fields  := TFDQuery(DsPes.DataSet).Fields[I].FieldName;
          Options := [soDescending];
          Active  := True;
        end;
      end;
    end;
  end;

  for I := 0 to UniDBGrid1.Columns.Count - 1 do
    UniDBGrid1.Columns[i].Sortable := True;
end;

try this code.

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...