Skip to main content

Example: TcxCustomGridView.StoreToRegistry, TcxCustomGridView.RestoreFromRegistry

  • 3 minutes to read

Assume that we have two data-aware Views (tvSource and tvTarget) and we need the second View to display the same data as the first (using the same View layout).

The settings of the first View are stored using the StoreToRegistry procedure. In order to be able to restore View settings to another View, set the ASaveViewName parameter to a non-empty string. This name will be used in the RestoreFromRegistry procedure when loading settings to the target View.

If you specify a name for the ASaveViewName parameter, items will be stored by their IDs. The restore procedure reads entries in the registry and applies them to corresponding items based on their ID values.

In order for the following code to work properly, items in the tvSource and tvTarget Views must have the same IDs.

When you are running the program, all items within a View are assigned unique zero-based IDs. The ID of the first item is 0, the ID of the second one is 1, etc. IDs provide the unique identity of the items within the View. When an item is deleted, a newly created item might get the ID of the deleted item. Thus the IDs of items won’t be reused within the current state of the View, but may be reused within the lifetime of the View. This is a limit unique to these IDs.

The code below is created with the following assumption:

  • IDs of the tvSource View items are sequential numbers. The ID of the first item is zero.

  • the tvTarget View contains no items before running the program. This ensures that the items created at runtime will get sequential zero-based IDs as well (the same as in the tvSource View).

var
  AStoreKey, ASaveViewName: string;
  I: Integer;
  AOptions: TcxGridStorageOptions;
//...
//The path to the layout
AStoreKey := 'Software\MyProjects\TestStoring';
//The name to refer to the stored settings
ASaveViewName := 'tvSource Layout';
AOptions := [];
//Create items in the tvTarget View
for I := 0 to tvSource.ItemCount - 1 do
  tvTarget.CreateItem;
//Save settings of the tvSource View
  tvSource.StoreToRegistry(AStoreKey, True, AOptions, ASaveViewName);
//Apply settings to the tvTarget View
tvTarget.RestoreFromRegistry(AStoreKey, False, False, AOptions, ASaveViewName);
//Make certain that the tvTarget View displays the same data as the tvSource View
tvTarget.DataController.DataSource := tvSource.DataController.DataSource;
See Also