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.
DevExpress Reports Prerequisites
- Run the sample app and click New Report.
- Click Design Report to display the Report Designer dialog.
- Create a report layout using tools available within the UI.
- Click the hamburger button, select the Save option, and close the dialog.
- Close and restart the app. Click Design Report or Preview Report to load the saved report in the Report Designer or Viewer.
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.
-
Add a TdxMemData component to the data module (
mdLayoutsin the example). -
Add a TDataSource component to the data module (
dsLayoutsin the example). Assign the previously created dataset component toTDataSource.DataSet: -
Open the context menu for the dataset component and select Field Editor…:
-
Click Add… to create a BLOB field for layout data:
-
Click Add… to create a string field for layout names:
-
(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.
Alternatively, you can use the Report Designer later to import report data from a file.
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.
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;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;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;- uData.pas/uData.cpp stores report layouts.
- uMainForm.pas/uMainForm.cpp creates a TdxReport, loads report layouts from the data module, and displays Report Designer/Viewer.
- data.dat stores the memory-based dataset state between application sessions.
- nwind.db contains the Northwind sample database used as a data source for report content.
- Introduction to VCL Reports
- Tutorial: Create a table report using the Report Wizard
- Use SQLite as a data source for reports (as demonstrated in the current example)
- Store report layouts in REPX files at design-time
- API reference:
- TdxReport.ReportName (internal report name that is not included in the layout)
- TdxReport.Layout (an XML-based layout template that can be stored in a BLOB data field)
- TdxMemData (DevExpress in-memory dataset implementation)
- TDataSet (contains generic database connection methods)
- TdxBackendDatabaseSQLConnection (supplies data to reports)
(you will be redirected to DevExpress.com to submit your response)




