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 ?
Lazarus - Zeos - DBGrid
Moderators: gto, cipto_kh, EgonHugeist
-
- Platinum Boarder
- Posts: 1956
- Joined: 17.01.2011, 14:17
Re: Lazarus - Zeos - DBGrid
Hello and welcome back
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:
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.
Best regards,
Jan
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?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 ...')
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;
Sorry, I don't know these controls.SunDoctor wrote: ↑30.06.2020, 15:11 May be someone has experience with KControls (https://wiki.freepascal.org/KControls) - TKDBGrid?
Best regards,
Jan