in Delphi TIPS :: When DBGrid is used to display data from a dataset (query or table), by design, after you call Refresh method on a dataset (re-open) (for example, using a DBNavigator), the current row position will be set to zero (first record).
If you have been asking "Is there any way to reopen or refresh a query, leaving the TDBGrid data exactly where it is (without changing the positions)?" Here's one answer to the problem...
Read the full article to learn how to Refresh DBGrid Data - Preserve Row Position.
Related:


I do this using Bookmark in the dataset
procedure RefreshDataSet(Dset: TIBDataSet);
var
P: Pointer;
begin
P := Dset.GetBookmark;
try
Dset.Close;
Dset.Open;
if dset.BookmarkValid(P) then
Dset.GotoBookmark(P);
finally
Dset.FreeBookmark(P);
end;
end;
Thank !
What if we have indexed field?
Isn’t Locate faster in that case?
Thanks a lot, very useful the the solution of bookmark.
As a previous poster mentioned, using a bookmark works like a champ! I had a master-detail relationship set up between two tables in which the master field was displayed a dbGrid. Once a new record was POST’d I wanted to immediate allow entry of linked data. Unfortunately, calling DS.Refresh moved the cursor back to the first record. I used a very similar approach to manage this:
procedure TFormMain.ButtonSaveMasterChanges (Sender: TObject);
var
bookmark : TBookmark;
begin
try
QueryMaster.DisableControls;
QueryMaster.Post;
bookmark := QueryMaster.GetBookmark;
QueryMaster.Refresh;
if QueryMaster.BookmarkValid(bookmark) then
QueryMaster.GotoBookmark(bookmark);
QueryContact.EnableControls;
finally
QueryMaster.FreeBookmark(bookmark);
end;
end;