Sharpie
Sharpie

Reputation: 383

Refresh Query / cxGrid without losing selected record

I understand that doing the following refreshes a query.

query.Close;
query.Open;

But after doing this it sets focus back to the very first record on the cxGrid.

Is there a way to make the current record remain selected after refreshing the query?

Thanks.

I have done the following..

procedure Tdatamodule.RefreshGrid;
var pos : tbookmark;
begin
pos := qryMainGrid.GetBookmark;
try
  qryMainGrid.Close;
  qryMainGrid.Open;
  qryMainGrid.GotoBookmark(pos);
finally
  qryMainGrid.FreeBookmark(pos);
end;

end;

But now get error message bookmark not found for dataset.

Any suggestions would be much appreciated.

Upvotes: 5

Views: 5320

Answers (1)

To refresh the dataset call the Refresh method and to remember the dataset cursor position use the bookmark. You query for the bookmark to the current cursor position by calling GetBookmark, refresh the dataset and move to the bookmarked position by calling GotoBookmark:

var
  Bookmark: TBookmark;
begin
  Bookmark := Query.GetBookmark;
  Query.Refresh;
  Query.GotoBookmark(Bookmark);
end;

You don't need to call FreeBookmark to free the bookmark in your Delphi version because the TBookmark type became a dynamic array and as such is managed by the compiler when it goes out of scope of the function.

Upvotes: 5

Related Questions