Skip to main content
All docs
V25.1
  • TcxCustomGridView.RestoreDataViewStateFromStream(TStream,TcxGridStorageOptions) Method

    Restores the previously saved data view state from a stream.

    Declaration

    procedure RestoreDataViewStateFromStream(AStream: TStream; AOptions: TcxGridStorageOptions); virtual;

    Parameters

    Name Type Description
    AStream TStream

    The source stream that contains previously saved data view state information at the current position.

    AOptions TcxGridStorageOptions

    A set of flags that correspond to individual user interaction states restored from the source stream (AStream).

    Remarks

    Call the StoreDataViewStateToStream procedure to save specified user interaction states (selection, focus, the scroll position, etc.) to a stream separately from the base data structure/layout. A subsequent RestoreDataViewStateFromStream call restores saved user interaction states from the stream.

    View and User Interaction States

    Pass a set of all required flags[1] as the AOptions parameter to store/restore corresponding grid View states:

    gsoFocusedItem | gsoFocusedRecord | gsoFocusedView
    Store/restore the focus position.
    gsoSelected
    Stores/restores selection.
    gsoTopRecord
    Stores/restores the grid View scroll position.
    gsoExpanded
    Stores/restores the expanded status for all records.
    gsoDetail
    Stores/restores the active detail grid View. Applicable only to master-detail grid View relationships.

    Code Example: Store Data Layout and User Interaction States Separately

    The code example in this section demonstrates form OnDestroy and OnCreate event handlers. These handlers call StoreDataViewStateToStream/RestoreDataViewStateFromStream and StoreToStream/RestoreFromStream procedure pairs to store the data layout and user interaction states (selection, focus, scroll position, etc.) in different files (GridLayout.dat and GridUserInteraction.dat).

    uses
      System.SysUtils,  // Declares the FileExists function
      cxGrid,  // Declares the TcxGrid control
      cxGridCustomView,  // Declares the TcxCustomGridView class and related types
      cxGridDBTableView;  // Declares the TcxGridDBTableView class
    // ...
    
    procedure TMyForm.FormCreate(Sender: TObject);
    var
      AFileStream: TFileStream;
    begin
      // Restore the grid View data layout
      if FileExists('GridLayout.dat') then
      begin
        AFileStream := TFileStream.Create('GridLayout.dat', fmOpenReadWrite);
        try
          cxGrid1DBTableView1.RestoreFromStream(AFileStream);
        finally
          AFileStream.Free;
        end;
      end;
      // Restore grid View user interaction states
      if FileExists('GridUserInteraction.dat') then
      begin
        AFileStream := TFileStream.Create('GridUserInteraction.dat', fmOpenReadWrite);
        try
          cxGrid1DBTableView1.RestoreDataViewStateFromStream(AFileStream, cxGridStoreAllDataViewStates);
        finally
          AFileStream.Free;
        end;
      end;
    end;
    
    procedure TMyForm.FormDestroy(Sender: TObject);
    var
      AFileStream: TFileStream;
    begin
      // Store the grid View data layout
      AFileStream := TFileStream.Create('GridLayout.dat', fmCreate or fmOpenReadWrite);
      try
        cxGrid1DBTableView1.StoreToStream(AFileStream);
      finally
        AFileStream.Free;
      end;
      // Store grid View user interaction states
      AFileStream := TFileStream.Create('GridUserInteraction.dat', fmCreate or fmOpenReadWrite);
      try
        cxGrid1DBTableView1.StoreDataViewStateToStream(AFileStream, cxGridStoreAllDataViewStates);
      finally
        AFileStream.Free;
      end;
    end;
    

    Other View State Store/Restore Methods

    Alternatively, you can store grid View data layout and user interaction states in an INI file, system registry, or custom storage. The TcxCustomGridView class implements the following Store~/Restore~ method pairs in addition to StoreDataViewStateToStream and RestoreDataViewStateFromStream:

    StoreToIniFile | RestoreFromIniFile
    Allow you to store the grid View state (both data layout and user interaction states) between sessions in an INI file.
    StoreToRegistry | RestoreFromRegistry
    Allow you to store the grid View state (both data layout and user interaction states) between sessions in the system registry.
    StoreToStorage | RestoreFromStorage
    Allow you to store the grid View state between sessions in a custom storage.
    StoreToStream | RestoreFromStream
    Allow you to store grid View state (both data layout and user interaction states) in a stream.
    StoreDataViewState | RestoreDataViewState
    Allow you to store user interaction states in memory during the same session.
    Footnotes
    1. Alternatively, you can use only the cxGridStoreAllDataViewStates constant if you need to store all user interaction states for the grid View as demonstrated in the code example in this topic.

    See Also