|
4 | 4 | [](https://docs.devexpress.com/GeneralInformation/403183) |
5 | 5 | [](#does-this-example-address-your-development-requirementsobjectives) |
6 | 6 | <!-- default badges end --> |
7 | | -# DevExpress VCL Reports - Store Report Layouts in a Database |
8 | 7 |
|
9 | | -This example stores a [report layout](https://docs.devexpress.com/VCL/dxReport.TdxReport.Layout) (XML-based template) in the BLOB field of a memory-based dataset ([TdxMemData](https://docs.devexpress.com/VCL/dxmdaset.TdxMemData) inherited from the [TDataSet](https://docwiki.embarcadero.com/Libraries/Athens/en/Data.DB.TDataSet) class shipped with the standard VCL library). |
| 8 | +# DevExpress Reports for Delphi/C++Builder – Store Report Layouts in a Database |
10 | 9 |
|
11 | | -## Testing the example |
| 10 | +This example application stores [DevExpress report layouts][TdxReport.Layout] in a database. |
| 11 | +The application allows users to create and save report layouts, modify existing layouts, and open layouts in Report Designer/Viewer. |
| 12 | + |
| 13 | +To store a DevExpress Report layout to a database, you need to store the following two properties of the [TdxReport] component: |
| 14 | + |
| 15 | +- [TdxReport.Layout] – an XML-based representation of the report template. |
| 16 | + This property is a `TStringList` and requires a `TBlobField` to store. |
| 17 | +- [TdxReport.ReportName] – the internal name of the layout, not included in the layout data. |
| 18 | + This value is a `string` and can be stored in a `TWideStringField`. |
| 19 | + |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +[DevExpress Reports Prerequisites][req] |
| 24 | + |
| 25 | +[req]: https://docs.devexpress.com/VCL/405773/ExpressCrossPlatformLibrary/vcl-backend/reports-dashboards-app-deployment#vcl-reportsdashboards-prerequisites |
| 26 | + |
| 27 | + |
| 28 | +## Test the Example |
| 29 | + |
| 30 | +- Run the sample app and click **New Report** to create an empty database record. |
| 31 | +- Click **Show Designer** to display the [Report Designer][dx-report-designer] dialog. |
| 32 | +- Create a report layout using tools available within the UI. |
| 33 | +- Click the hamburger button, select the **Save** option, and close the dialog. |
| 34 | +- Close the app. The [TdxMemData] component will store layout data in the [data.dat] file between sessions. |
| 35 | +- Run the sample app again. Click **View Designer** to load the saved report layout, |
| 36 | + or **View Report** to preview a layout-based report in the [Report Viewer][dx-report-viewer] dialog. |
| 37 | + |
| 38 | + |
| 39 | +## Implementation Details |
| 40 | + |
| 41 | +The example uses a memory-based dataset |
| 42 | +([TdxMemData] from the DevExpress library, inherited from [TDataSet], shipped with the standard VCL library). |
| 43 | + |
| 44 | +Follow the steps below to store report layouts in a database of your choice, using the corresponding `TDataSet` descendant. |
| 45 | + |
| 46 | +> Note: |
| 47 | +> Applications in this example isolate data components in separate data modules: [uData.pas] (Delphi) and [uData.cpp] (C++Builder). |
| 48 | +
|
| 49 | +### Step 1: Create a Dataset to Store Report Layout Data |
| 50 | + |
| 51 | + |
| 52 | +Follow these steps to create a memory-based dataset to store report layout data: |
| 53 | + |
| 54 | +1. Create a [TdxMemData] component on a form (`mdLayouts` in the example). |
| 55 | +2. Create a [TDataSource] component on a form (`dsLayouts` in the example). |
| 56 | + Assign the `TDataSource.DataSet` value to the previously created dataset component: |
| 57 | + |
| 58 | +  |
| 59 | +3. Open the context menu and select **Field Editor…**: |
| 60 | + |
| 61 | +  |
| 62 | +4. Click **Add…** to create a BLOB field for layout data: |
| 63 | + |
| 64 | + |
| 65 | +### Step 2: Supply Layout Data to TdxReport |
| 66 | + |
| 67 | +[TdxReport] requires a [Layout][TdxReport.Layout] and [ReportName][TdxReport.ReportName] |
| 68 | +to open the Viewer and Designer dialogs. |
| 69 | +Assign both properties by reading them from the dataset: |
| 70 | + |
| 71 | + |
| 72 | +### Step 3: Display Report Designer and Viewer |
| 73 | + |
| 74 | +### Step 4: Handle Layout Changes |
| 75 | + |
| 76 | +When a user edits and saves a report in the Report Designer, |
| 77 | +the value of [TdxReport.Layout] changes and a `LayoutChanged` event is called. |
| 78 | +Handle this event to save the updated layout to the active dataset record. |
| 79 | + |
| 80 | + |
| 81 | +``` |
| 82 | +procedure TMainForm.dxReport1LayoutChanged(ASender: TdxReport); |
| 83 | +begin |
| 84 | + // Start editing the active dataset record |
| 85 | + DataModule1.mdLayouts.Edit; |
| 86 | + // Save the report layout to the database |
| 87 | + DataModule1.mdLayoutsLayout.Assign(dxReport1.Layout); |
| 88 | + // Save the report name which is not included in the layout |
| 89 | + DataModule1.mdLayoutsName.AsString := dxReport1.ReportName; |
| 90 | + // Finish editing and write a modified record |
| 91 | + DataModule1.mdLayouts.Post; |
| 92 | +end; |
| 93 | +``` |
| 94 | + |
| 95 | +### Step 5: Save the Database State when Application Exists |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +## Files to Review |
| 100 | + |
| 101 | +- [uData.pas] (Delphi) and [uData.cpp] (C++Builder) read and store layout data in a database represented by an in-memory storage component. |
| 102 | +- [data.dat] stores layout data between application sessions. |
| 103 | +- [uMainForm.pas] (Delphi) and [uMainForm.cpp] (C++Builder) supply layout data from the data module to [TdxReport.Layout] and display Report Designer and Viewer. |
| 104 | +- [nwind.db] contains the Northwind sample database used as a data source for report content. |
| 105 | + |
| 106 | +[uData.pas]: ./Delphi/uData.pas |
| 107 | +[uData.cpp]: ./CPB/uData.cpp |
| 108 | +[data.dat]: ./Delphi/data.dat |
| 109 | +[uMainForm.pas]: ./Delphi/uMainForm.pas |
| 110 | +[uMainForm.cpp]: ./CPB/uMainForm.cpp |
| 111 | +[nwind.db]: ./Delphi/nwind.db |
12 | 112 |
|
13 | | -* Run the sample app and click **New Report** to create an empty database record. |
14 | | -* Click **Show Designer** to display the [Report Designer](https://docs.devexpress.com/XtraReports/119176/web-reporting/web-end-user-report-designer) dialog. |
15 | | -* Create a report layout using tools available within the UI. |
16 | | -* Click the hamburger button, select the **Save** option, and close the dialog. |
17 | | -* Close the app. The [TdxMemData](https://docs.devexpress.com/VCL/dxmdaset.TdxMemData) component will store layout data between sessions. |
18 | | -* Run the sample app again. Click **View Designer** to load the saved report layout, or **View Report** to preview a layout-based report in the [Report Viewer](https://docs.devexpress.com/XtraReports/401850/web-reporting/web-document-viewer) dialog. |
19 | 113 |
|
20 | 114 | ## Documentation |
21 | 115 |
|
22 | | -* [TdxReport.Layout Property](https://docs.devexpress.com/VCL/dxReport.TdxReport.Layout) |
23 | | -* [TdxBackendDataSetJSONConnection Component](https://docs.devexpress.com/VCL/dxBackend.ConnectionString.JSON.DataSet.TdxBackendDataSetJSONConnection) |
| 116 | +- [Introduction to VCL Reports][reports-intro] |
| 117 | +- [Tutorial: Create a table report using the Report Wizard][report-wizard] |
| 118 | +- [Use SQLite as a data source for reports (as demonstrated in the current example)][sqlite-data-source] |
| 119 | +- [Store report layouts in REPX files at design-time][reports-design-time-store] |
| 120 | +- API reference: |
| 121 | + - [TdxReport.ReportName] |
| 122 | + - [TdxReport.Layout] (an XML-based layout template that can be stored in the BLOB field of a database) |
| 123 | + - [TdxReport.ReportName] |
| 124 | + - [TdxMemData] (used to store report layout data in application runtime and between sessions) |
| 125 | + - [TDataSet] (ancestor of the `TdxMemData`, contains generic database connection methods) |
| 126 | + - [TdxBackendDatabaseSQLConnection] (used to supply data to reports) |
| 127 | + |
| 128 | +<!-- documentation links --> |
| 129 | +[reports-intro]: https://docs.devexpress.com/VCL/405469/ExpressReports/vcl-reports |
| 130 | +[report-wizard]: https://docs.devexpress.com/VCL/405760/ExpressReports/getting-started/create-table-report-using-report-wizard |
| 131 | +[sqlite-data-source]: https://docs.devexpress.com/VCL/405750/ExpressCrossPlatformLibrary/vcl-backend/database-engines/vcl-backend-sqlite-support |
| 132 | +[reports-design-time-store]: https://docs.devexpress.com/VCL/dxReport.TdxReport.Layout#string-list-editor |
| 133 | +[dx-report-viewer]: https://docs.devexpress.com/XtraReports/401850/web-reporting/web-document-viewer |
| 134 | +[dx-report-designer]: https://docs.devexpress.com/XtraReports/119176/web-reporting/web-end-user-report-designer |
| 135 | + |
| 136 | + |
| 137 | +<!-- reference links --> |
| 138 | +[TdxReport]: https://docs.devexpress.com/VCL/dxReport.TdxReport |
| 139 | +[TdxReport.Layout]: https://docs.devexpress.com/VCL/dxReport.TdxReport.Layout |
| 140 | +[TdxReport.ReportName]: https://docs.devexpress.com/VCL/dxReport.TdxReport.ReportName |
| 141 | +[TdxBackendDatabaseSQLConnection]: https://docs.devexpress.com/VCL/dxBackend.ConnectionString.SQL.TdxBackendDatabaseSQLConnection |
| 142 | +[TdxBackendDataSetJSONConnection]: https://docs.devexpress.com/VCL/dxBackend.ConnectionString.JSON.DataSet.TdxBackendDataSetJSONConnection |
| 143 | +[TdxMemData]: https://docs.devexpress.com/VCL/dxmdaset.TdxMemData |
| 144 | +[TDataSet]: https://docwiki.embarcadero.com/Libraries/Athens/en/Data.DB.TDataSet |
| 145 | + |
| 146 | +<!-- external documentation links --> |
| 147 | +<!-- in-repository links --> |
| 148 | + |
| 149 | + |
| 150 | +## More Examples |
| 151 | + |
| 152 | +- [Store report layouts in REPX files][file-example] |
| 153 | + |
| 154 | +[file-example]: https://github.com/DevExpress-Examples/vcl-reports-store-layout-template-file |
| 155 | + |
| 156 | + |
24 | 157 | <!-- feedback --> |
25 | 158 | ## Does This Example Address Your Development Requirements/Objectives? |
26 | 159 |
|
27 | 160 | [<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=vcl-reports-store-layout-template-database&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=vcl-reports-store-layout-template-database&~~~was_helpful=no) |
28 | 161 |
|
29 | 162 | (you will be redirected to DevExpress.com to submit your response) |
30 | 163 | <!-- feedback end --> |
31 | | - |
32 | | - |
33 | | - |
|
0 commit comments