1

Using Delphi XE2.

I am writing a software package which uses cxGrids and are linked to Querys/Datasources.

At a click of a button how do you refresh a Query to make sure the records are up to date in the cxGrids.

Also if a record is highlighted on a cxGrid it must remember that record and doesn't reset back to the top of the grid.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Sharpie
  • 373
  • 2
  • 15
  • 34

3 Answers3

2

Close and open the dataset behind the cxgrid to make sure you have the latest data.

dataset.close;
dataset.open;

If you need to remember the current record and put the cursor back on it - use a bookmark as shown in the link below.

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TDataSet_GetBookmark.html

If you prefer not to use a bookmark, you can utilise the dataset.locate method. Store the primary key of the record, and after the refresh, use dataset.locate(dataset.fieldbyname('PK').AsDataType) to take you back to that record.

Using the locate method is probably a more readable/elegant way of working.

MrRobot
  • 169
  • 3
  • 10
1

for cxgrid of devexpress the bookmaker is a bad solution for restore the selection , you can use the cxStatusKeeper ( it public unit that you can download from support center of devexpress )

{Init the component for restore selection}
FGridStatus := TcxGridDBTableKeeper.Create(self);
FGridStatus.LoadExpanding   := False;
FGridStatus.LoadSelection   := True;
FGridStatus.LoadFocus       := True;
FGridStatus.LoadTopRecord   := False;
FGridStatus.LoadWithDetails := False;
FGridStatus.LoadFocusedView := True;
FGridStatus.LoadFocusedItem := True;
FGridStatus.View            := gvTableElementi;

{save the current items} 
FGridStatus.Store;

{restore the selection}   
if FGridStatus.GridStored then
  FGridStatus.Restore;
  • For completeness, I'll add the link to the DevExpress article: https://supportcenter.devexpress.com/ticket/details/ka18654/how-to-save-and-then-restore-the-grid-s-and-treelist-s-current-state-extended-version – Alexandre M Oct 01 '20 at 21:27
0

cxGridTableView.Navigator has a Refresh button that will do what you want.

If you want to Refresh using your own button, you can call cxGridTableView.DataController.RefreshExternalData

crefird
  • 1,590
  • 11
  • 17