Skip to content

DevExpress-Examples/vcl-reports-store-layout-template-database

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DevExpress Reports for Delphi/C++Builder – Store Report Layouts in a Database

This example application stores DevExpress report layouts in a database. The sample allows users to create new layouts, modify existing layouts using the built-in Report Designer, and save changes to the data source.

Prerequisites

DevExpress Reports Prerequisites

Test the Example

  1. Run the sample app and click New Report.
  2. Click Design Report to display the Report Designer dialog.
    1. Create a report layout using tools available within the UI.
    2. Click the hamburger button, select the Save option, and close the dialog.
  3. Close and restart the app. Click Design Report or Preview Report to load the saved report in the Report Designer or Viewer.

Implementation Details

The example uses a DevExpress memory-based dataset for report layout storage: TdxMemData. You can modify the application to use any other TDataSet descendant instead. To review our data module implementation, see the following file: uData.pas/uData.cpp.

The instructions assume that you start with a Delphi or C++Builder project that already includes a configured data source for DevExpress Reports. To configure a report data source in your project, refer to the following tutorial: Create a Table Report Using the Report Wizard. This example project uses a SQLite sample database (nwind.db) as the report's data source.

Step 1: Create a Dataset to Store Report Layout Data

  1. Add a TdxMemData component to the data module (mdLayouts in the example).

  2. Add a TDataSource component to the data module (dsLayouts in the example). Assign the previously created dataset component to TDataSource.DataSet:

    Object Inspector panel displaying TDataSource properties.

  3. Open the context menu for the dataset component and select Field Editor…:

    Context menu for the TdxMemData component displaying a 'Field Editor' option.

  4. Click Add… to create a BLOB field for layout data:

    New Field dialog adding a 'Layout' field of type ftBlob

  5. Click Add… to create a string field for layout names:

    New Field dialog adding a 'Name' field of type ftWideString

  6. (Optional) Preload persistent data to the dataset to make layouts available in the application upon first launch.

    This example includes a sample report layout that displays data from the Northwind sample database. You can preload it from example.dat. Open the context menu for the dataset component, select Persistent Editor…, click Load…, and select the file.

    Context menu for the TdxMemData component displaying a 'Persistent Editor' option.

    Alternatively, you can use the Report Designer later to import report data from a file.

Step 2: Load a Report Layout Definition

To load a layout definition to the TdxReport component, you must specify report name (TdxReport.ReportName) and layout (TdxReport.Layout):

procedure TMainForm.LoadReportNameAndLayout();
begin
  // Ensure that the dataset has at least one record or a new record is being created:
  if (DataModule1.mdLayouts.RecordCount = 0) and not (DataModule1.mdLayouts.State = dsInsert) then
  begin
    ShowMessage('The database is empty');
    Exit;
  end;
  // Load report name and layout from the database:
  dxReport1.ReportName := DataModule1.mdLayoutsName.AsString;
  dxReport1.Layout.Assign(DataModule1.mdLayoutsLayout);
end;

To load a different report, assign a new report name and layout. The assigned report replaces the current layout definition.

Step 3: Display Report Designer and Viewer

Once you assigned a name and layout to the TdxReport component, you can display Report Designer and Report Viewer dialogs:

procedure TMainForm.btnDesignClick(Sender: TObject);
begin
  LoadReportFromLayout; // Loads a report layout definition from the database
  dxReport1.ShowDesigner; // Displays the Report Designer
end;

procedure TMainForm.btnPreviewClick(Sender: TObject);
begin
  LoadReportFromLayout; // Loads a report layout definition from the database
  dxReport1.ShowViewer; // Displays the Report Viewer
end;

Step 4: Store Report Layouts in a Dataset

When a user edits and saves a report in the Report Designer, the value of TdxReport.Layout changes and an OnLayoutChanged event is called. Handle this event to save layout changes to the active dataset record.

procedure TMainForm.dxReport1LayoutChanged(ASender: TdxReport);
begin
  // Start editing the active dataset record:
  DataModule1.mdLayouts.Edit;

  // Save report name and layout to the database:
  DataModule1.mdLayoutsName.AsString := dxReport1.ReportName;
  DataModule1.mdLayoutsLayout.Assign(dxReport1.Layout);

  // Finish editing and post the modified record to the database:
  DataModule1.mdLayouts.Post;
end;

Step 5: Persist Data between Application Sessions

This step is applicable only to the memory-based TdxMemData datasource.

To save the dataset to a file and restore data on app restart, handle OnCreate and OnDestroy events of the data module:

const
  DataFileName = 'data.dat';

procedure TDataModule1.DataModuleCreate(Sender: TObject);
begin
  if FileExists(DataFileName) then
    mdLayouts.LoadFromBinaryFile(DataFileName)
end;

procedure TDataModule1.DataModuleDestroy(Sender: TObject);
begin
  if mdLayouts.RecordCount > 0 then
    mdLayouts.SaveToBinaryFile(DataFileName)
end;

Files to Review

Documentation

More Examples

Does This Example Address Your Development Requirements/Objectives?

(you will be redirected to DevExpress.com to submit your response)

About

This example stores a report layout (XML-based template) in the BLOB field of a memory-based dataset (TdxMemData inherited from the TDataSet class shipped with the standard VCL library).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors