Lazarus - Zeos - DBGrid

In this forum you may discuss all issues concerning the Lazarus IDE and Freepascal (both running on Windows or Linux).

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
SunDoctor
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 18.03.2020, 16:59

Lazarus - Zeos - DBGrid

Post by SunDoctor »

Hi! Please help me to solve such a problem with Lazarus DBGrid

I wish to save cursor position (row and column) and scrolling position in DBGrid after Refresh() or Delete() with TZQuery (SELECT * FROM t)
Usually the cursor moves to the first row after .Refresh() or TZQuery.Exec('DELETE ...')
I try some code hacks like

THackDBGrid = class(TDBGrid);

ds := THackDBGrid(DBGrid1).DataSource.DataSet;
rowDelta := -1 + THackDBGrid(DBGrid1).Row;
SavePlace := ds.RecNo;
ds.Refresh;
with ds do
begin
DisableControls;
RecNo := SavePlace;
MoveBy(-rowDelta) ;
MoveBy(rowDelta) ;
EnableControls;
end

but they does not work when I have TZQuery.Filtered=true or TZQuery.IndexFieldNames='some field to sort'

May be someone has experience with KControls (https://wiki.freepascal.org/KControls) - TKDBGrid?

Or what is the correct way to save cursor/scroll position after Delete/Refresh ?
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1956
Joined: 17.01.2011, 14:17

Re: Lazarus - Zeos - DBGrid

Post by marsupilami »

Hello and welcome back ;)


SunDoctor wrote: 30.06.2020, 15:11 Hi! Please help me to solve such a problem with Lazarus DBGrid

I wish to save cursor position (row and column) and scrolling position in DBGrid after Refresh() or Delete() with TZQuery (SELECT * FROM t)
Usually the cursor moves to the first row after .Refresh() or TZQuery.Exec('DELETE ...')
I assume you mean TZQuery.Delete instead of "TZQuery.Exec('DELETE ...')") But then - usually I would assume that calling .Delete would place you at the row just before the deleted row or the row after the deleted row - but not the first row?

Being placed at the first row after calling Refresh() is normal because it really opens and closes the dataset internally.

So - for remembering the current row, I usually remember the primary key of the row. The column is something different - because it is only known to the DBGrid. So - in Delphi my Code would look something like this:

Code: Select all

procedure RefreshData;
var
  Row: String;
  Column: Integer;
begin
  Row := Query.FieldByName('PrimaryKeyField').AsString; // I mostly use string encoded GUIDs as Primary keys
  Column := Grid.SelectedIndex;
  Query.DisableControls;
  try
    Query.Refresh;
    Query.Locate('PrimaryKeyField', Row, []);
  finally
    Query.EnableControls;
  end:
  Grid.SelectedIndex := Column;
When it comes to the DBGrid, I am not 100% sure, if Lazarus also has a SelectedIndex property. But I assume, they at least have something similar.
SunDoctor wrote: 30.06.2020, 15:11 May be someone has experience with KControls (https://wiki.freepascal.org/KControls) - TKDBGrid?
Sorry, I don't know these controls.

Best regards,

Jan
Post Reply