Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 768
Sharpie Avatar asked Apr 28 '15 11:04

Sharpie


1 Answers

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.

like image 95
Germán Estévez -Neftalí- Avatar answered Sep 29 '22 20:09

Germán Estévez -Neftalí-