Skip to content

Commit 0684852

Browse files
committed
readme: draft
1 parent eb23b42 commit 0684852

8 files changed

Lines changed: 165 additions & 16 deletions

Delphi/uData.dfm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ object DataModule1: TDataModule1
55
Width = 740
66
PixelsPerInch = 192
77
object mdLayouts: TdxMemData
8-
Active = True
98
Indexes = <>
109
Persistent.Data = {
1110
5665728FC2F5285C8FFE3F02000000000000000D0007004C61796F7574001400
@@ -795,7 +794,7 @@ object DataModule1: TDataModule1
795794
0072007400}
796795
SortOptions = []
797796
Left = 408
798-
Top = 112
797+
Top = 104
799798
object mdLayoutsLayout: TBlobField
800799
FieldName = 'Layout'
801800
end

Delphi/uMainForm.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ procedure TMainForm.btnPreviewClick(Sender: TObject);
7979
dxReport1.ShowViewer;
8080
end;
8181

82+
// Event that is called when a user saves a report layout in the Report Designer
8283
procedure TMainForm.dxReport1LayoutChanged(ASender: TdxReport);
8384
begin
8485
DataModule1.mdLayouts.Edit;
86+
// Copy the report layout data to the BLOB field
8587
DataModule1.mdLayoutsLayout.Assign(dxReport1.Layout);
88+
// Save the report name which is not included in the layout
8689
DataModule1.mdLayoutsName.AsString := dxReport1.ReportName;
8790
DataModule1.mdLayouts.Post;
8891
end;

README.md

Lines changed: 161 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,177 @@
44
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
55
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
66
<!-- default badges end -->
7-
# DevExpress VCL Reports - Store Report Layouts in a Database
87

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
109

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+
> <img src="./images/create-bind-data-source.png" style="width: 50%" alt=""/>
59+
60+
3. Open the context menu of the dataset component and select **Field Editor…**:
61+
62+
> <img src="./images/open-context-menu.png" style="width: 50%" alt=""/>
63+
64+
4. Click **Add…** to create a BLOB field for layout data:
65+
66+
> <img src="./images/create-layout-field.png" style="width: 50%" alt=""/>
67+
68+
5. Click **Add…** to create a string field for layout name:
69+
70+
> <img src="./images/create-name-field.png" style="width: 50%" alt=""/>
71+
72+
6. (*Optional*) Preload persistent data to the dataset make layouts available in the application upon first launch.
73+
Open the context menu of the dataset component, select **Persistent Editor…**, click **Load…** and select a DAT file.
74+
75+
This example has an example report layout designed to use data from the Northwind sample database.
76+
You can preload it from [example.dat](./Delphi/example.dat).
77+
78+
Alternatively, you can use the Report Designer in further steps to import report data from files or raw string data.
79+
80+
> <img src="./images/create-persistent-data.png" style="width: 50%" alt=""/>
81+
82+
83+
### Step 2: Supply Layout Data to TdxReport
84+
85+
[TdxReport] requires a [Layout][TdxReport.Layout] and [ReportName][TdxReport.ReportName].
86+
Assign both properties by reading them from the dataset:
87+
88+
89+
### Step 3: Display Report Designer and Viewer
90+
91+
### Step 4: Handle Layout Changes
92+
93+
When a user edits and saves a report in the Report Designer,
94+
the value of [TdxReport.Layout] changes and a `LayoutChanged` event is called.
95+
Handle this event to save the updated layout to the active dataset record.
96+
97+
98+
```pas
99+
procedure TMainForm.dxReport1LayoutChanged(ASender: TdxReport);
100+
begin
101+
// Start editing the active dataset record
102+
DataModule1.mdLayouts.Edit;
103+
// Save the report layout to the database
104+
DataModule1.mdLayoutsLayout.Assign(dxReport1.Layout);
105+
// Save the report name which is not included in the layout
106+
DataModule1.mdLayoutsName.AsString := dxReport1.ReportName;
107+
// Finish editing and write a modified record
108+
DataModule1.mdLayouts.Post;
109+
end;
110+
```
111+
112+
### Step 5: Save the Database State when Application Exists
113+
114+
115+
116+
## Files to Review
117+
118+
- [uData.pas] (Delphi) and [uData.cpp] (C++Builder) read and store layout data in a database represented by an in-memory storage component.
119+
- [data.dat] stores layout data between application sessions.
120+
- [uMainForm.pas] (Delphi) and [uMainForm.cpp] (C++Builder) supply layout data from the data module to [TdxReport.Layout] and display Report Designer and Viewer.
121+
- [nwind.db] contains the Northwind sample database used as a data source for report content.
122+
123+
[uData.pas]: ./Delphi/uData.pas
124+
[uData.cpp]: ./CPB/uData.cpp
125+
[data.dat]: ./Delphi/data.dat
126+
[uMainForm.pas]: ./Delphi/uMainForm.pas
127+
[uMainForm.cpp]: ./CPB/uMainForm.cpp
128+
[nwind.db]: ./Delphi/nwind.db
12129

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.
19130

20131
## Documentation
21132

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)
133+
- [Introduction to VCL Reports][reports-intro]
134+
- [Tutorial: Create a table report using the Report Wizard][report-wizard]
135+
- [Use SQLite as a data source for reports (as demonstrated in the current example)][sqlite-data-source]
136+
- [Store report layouts in REPX files at design-time][reports-design-time-store]
137+
- API reference:
138+
- [TdxReport.ReportName]
139+
- [TdxReport.Layout] (an XML-based layout template that can be stored in the BLOB field of a database)
140+
- [TdxReport.ReportName]
141+
- [TdxMemData] (used to store report layout data in application runtime and between sessions)
142+
- [TDataSet] (ancestor of the `TdxMemData`, contains generic database connection methods)
143+
- [TdxBackendDatabaseSQLConnection] (used to supply data to reports)
144+
145+
<!-- documentation links -->
146+
[reports-intro]: https://docs.devexpress.com/VCL/405469/ExpressReports/vcl-reports
147+
[report-wizard]: https://docs.devexpress.com/VCL/405760/ExpressReports/getting-started/create-table-report-using-report-wizard
148+
[sqlite-data-source]: https://docs.devexpress.com/VCL/405750/ExpressCrossPlatformLibrary/vcl-backend/database-engines/vcl-backend-sqlite-support
149+
[reports-design-time-store]: https://docs.devexpress.com/VCL/dxReport.TdxReport.Layout#string-list-editor
150+
[dx-report-viewer]: https://docs.devexpress.com/XtraReports/401850/web-reporting/web-document-viewer
151+
[dx-report-designer]: https://docs.devexpress.com/XtraReports/119176/web-reporting/web-end-user-report-designer
152+
153+
154+
<!-- reference links -->
155+
[TdxReport]: https://docs.devexpress.com/VCL/dxReport.TdxReport
156+
[TdxReport.Layout]: https://docs.devexpress.com/VCL/dxReport.TdxReport.Layout
157+
[TdxReport.ReportName]: https://docs.devexpress.com/VCL/dxReport.TdxReport.ReportName
158+
[TdxBackendDatabaseSQLConnection]: https://docs.devexpress.com/VCL/dxBackend.ConnectionString.SQL.TdxBackendDatabaseSQLConnection
159+
[TdxMemData]: https://docs.devexpress.com/VCL/dxmdaset.TdxMemData
160+
161+
<!-- external documentation links -->
162+
[TDataSet]: https://docwiki.embarcadero.com/Libraries/Athens/en/Data.DB.TDataSet
163+
[TDataSource]: https://docwiki.embarcadero.com/Libraries/Athens/en/Data.DB.TDataSource
164+
<!-- in-repository links -->
165+
166+
167+
## More Examples
168+
169+
- [Store report layouts in REPX files][file-example]
170+
171+
[file-example]: https://github.com/DevExpress-Examples/vcl-reports-store-layout-template-file
172+
173+
24174
<!-- feedback -->
25175
## Does This Example Address Your Development Requirements/Objectives?
26176

27177
[<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)
28178

29179
(you will be redirected to DevExpress.com to submit your response)
30180
<!-- feedback end -->
31-
32-
33-

images/create-bind-data-source.png

26.7 KB
Loading

images/create-layout-field.png

33.9 KB
Loading

images/create-name-field.png

34.2 KB
Loading

images/create-persistent-data.png

13.5 KB
Loading

images/open-context-menu.png

13.1 KB
Loading

0 commit comments

Comments
 (0)