Find next record in sorted TDBGrid
<p>I have TDBGrid on witch I have connected FDQuery. I sort records on fly with  use of FDQuery indexes when column title is clicked. </p>

<pre><code>procedure TMainDM.CreateIndexes(AQuery: TFDQuery);
var
  I: Integer;
begin
  try
    AQuery.Indexes.Clear;
    for I := 0 to AQuery.FieldCount - 1 do
    begin
      with AQuery.Indexes.Add do
      begin
        Name := AQuery.Fields[i].FieldName + '_index_asc';
        Fields:= AQuery.Fields[i].FieldName;
        Options:= [soNoCase];
        Active:= True;
      end;
      with AQuery.Indexes.Add do
      begin
        Name := AQuery.Fields[i].FieldName + '_index_desc';
        DescFields:= AQuery.Fields[i].FieldName;
        Fields:= AQuery.Fields[i].FieldName;
        Active:= True;
      end;
    end;
  except
    on E: Exception do
      ErrorLogger('ERROR: TMainDM.CreateIndexes -&gt; ' + E.Message);
  end;
end;
</code></pre>

<p>How would  I get next record from currently selected? I know that I move to next record of unsorted grid with this:</p>

<pre><code>DBGrid.DataSource.Dataset.Next;
</code></pre>

<p>I know that I move to next record in filtered grid with this:</p>

<pre><code>DBGrid.DataSource.Dataset.FindNext;
</code></pre>

<p>How can I find next record in sorted list? When I call Next or FindNext I get next record of unsorted grid.</p>

<h2><strong>EDIT 1</strong></h2>

<p>I had FDQuery property IndexName set to index selected in event OnColumnSort.</p>

<pre><code>if Direction then
  TFDQuery(TaskDataSource.DataSet).IndexName:= Column.FieldName + '_index_asc'
else
  TFDQuery(TaskDataSource.DataSet).IndexName:= Column.FieldName + '_index_desc';
</code></pre>

<p>I get result described above. In first code block I create indexes for all fields so they are ready to be called in seconde code block (event OnColumnSort)</p>

<h2><strong>EDIT 2</strong></h2>

<p>I've changed some code. I put index creation in the event OnColumnSort so there isn't index for every field on start. Query.Next still doesn't work.</p>

<pre><code>procedure TMain.TaskGridColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  try
    TFDQuery(TaskDataSource.DataSet).Indexes.Clear;
    if Direction then
    begin
      with TFDQuery(TaskDataSource.DataSet).Indexes.Add do
      begin
        Name := Column.FieldName + '_index_asc';
        Fields:= Column.FieldName;
        Options:= [soNoCase];
        Active:= True;
      end;
      TFDQuery(TaskDataSource.DataSet).IndexName:= Column.FieldName + '_index_asc';
    end
    else
    begin
      with TFDQuery(TaskDataSource.DataSet).Indexes.Add do
      begin
        Name := Column.FieldName + '_index_desc';
        DescFields:= Column.FieldName;
        Fields:= Column.FieldName;
        //Options:= [soDescending, soNoCase];
        Active:= True;
      end;
      TFDQuery(TaskDataSource.DataSet).IndexName:= Column.FieldName + '_index_desc';
    end;

  except
    on E: Exception do
      MainDM.ErrorLogger('ERROR: TMain.TaskGridColumnSort -&gt; ' + E.Message);
  end;
</code></pre>

<p>end;</p>