1. Computing & Technology

Discuss in my forum

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:

Comments
May 12, 2008 at 2:33 pm
(1) Daniel Alves Grillo says:

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;

August 9, 2009 at 5:32 am
(2) drugda says:

Thank !

November 13, 2009 at 11:30 am
(3) Dimitrij says:

What if we have indexed field?
Isn’t Locate faster in that case?

February 9, 2011 at 2:09 am
(4) Raul says:

Thanks a lot, very useful the the solution of bookmark.

February 3, 2012 at 9:15 pm
(5) JMF says:

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;

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2012 About.com. All rights reserved.

A part of The New York Times Company.