Always select Topmost Row after sorting in TTMSFNCDataGrid when connected to DatabaseAdapter

Is it possible to select the topmost row of the TTMSFNCDataGrid after sorting when the Grid has an TTMSFNCDataGridDatabaseAdapter. As of now it´s always staying at the selected Row.

I´ve tried changing the selection in the OnAfterSortColumn-Event but can´t fin a way to achieve this. (The TTMSFNCDataGrid.SelectCell does unfortunately not select the sorted index. It always selects the same cell reagrdless of the sorting).

Hi,

Right now, it selects and synchronizes with the active record, so if your active record is 1 and after sorting the active record jumps to 50, the grid will jump to the corresponding row. If you don't want this, you could reset the active record by calling

procedure TForm1.TMSFNCDataGrid1AfterSortColumn(Sender: TObject;
  AColumn: Integer);
begin
  ClientDataSet1.RecNo := 1;
end;

Which will jump to the first record in the dataset and this should correspond with the first row in the grid.

Alternatively you can also set the TopRow

procedure TForm1.TMSFNCDataGrid1AfterSortColumn(Sender: TObject;
  AColumn: Integer);
begin
//  ClientDataSet1.RecNo := 1;
  TMSFNCDataGrid1.TopRow := 0;
end;

OK, i´ve tried using the second option:

procedure TForm1.TMSFNCDataGrid1AfterSortColumn(Sender: TObject;
  AColumn: Integer);
begin
  TMSFNCDataGrid1.TopRow := 0;
end;

But unfortunately this doesn´t work when there is a Field with TFieldDef.DataType = ftMemo in the Dataset. In my case the Dataset is a TFDMemTable :thinking:

Small sample illustrating the problem:
Sample_TMSFNCDataGrid.zip (15.8 KB)

I've tested this here, but in all cases, the TopRow is correct. The sorting doesn't apply because there is no content in the memo field, so the sort operation doesn't work. TopRow only changes the scroll position, and if that affects the selected row, then it will also synchronize the selected row.

Yes, for the Memo-Field it works as expected but for the id it doesn´t as soon as there is a Memo-Field in the Dataset.

The Memo-Field just seems to be the cause for the issue when i´m sorting by other fields.

I´ve made a small screen recording of the issue. The Code is the same as the sample i´ve sent:
Sample Video.zip (3.5 MB)

Thanks for the video. We have different behavior here, but this could be due to some underlying changes we did in the database adapter related to sorting & filtering. Next version should have this fixed and is planned for next week.

Ok wait, I might be confused by the selected row, the TopRow property is not automatically selecting the top-most row. It's use to navigate in the grid. If you want this, you need to use this code instead:

procedure TForm1.TMSFNCDataGrid1AfterSortColumn(Sender: TObject; AColumn: Integer);
begin
  TMSFNCDataGrid1.TopRow := 0;
  TMSFNCDataGrid1.FocusedCell := MakeCell(TMSFNCDataGrid1.FocusedCell.Column, TMSFNCDataGrid1.TopRow);
end;

or, if you don't want to scroll all the way up, just select the top row, but not force it to 0

This works in the Sample i´ve sent but unfortunately not in my productive environment.

I guess i´ll wait for the "changes to the database adapter related to sorting & filtering" next week and report back if it works then.

Hi,

Good idea, don't hesitate to reach out if you still have issues after the update.