Skip to content

Commit a5eaeff

Browse files
committed
delphi: Rework example project
- Load the data module first to enable the main form to access the data in OnCreate event. - Aggregate the code for loading and saving layout definitions to separate methods. - Save layout changes to the dataset when a user saves a layout in the Report Designer. - Load layout from the dataset directly before showing report dialogs. - Use DevExpress message dialogs instead of standard VCL. - Remove the TdxSkinController component: DevExpress style settings are now always available without the need for a component. - Enable editing layout names in the grid view. Disable editing layouts. Enable deleting and moving records. - Remove unused modules in the `uses` section.
1 parent ae5a71e commit a5eaeff

7 files changed

Lines changed: 66 additions & 46 deletions

File tree

Delphi/StoreReportInDB.dpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ begin
1212
Application.Initialize;
1313
Application.MainFormOnTaskbar := True;
1414
Application.Title := 'DevExpress Example: Store Report in a Database';
15-
Application.CreateForm(TMainForm, MainForm);
1615
Application.CreateForm(TDataModule1, DataModule1);
16+
Application.CreateForm(TMainForm, MainForm);
1717
Application.Run;
1818
end.

Delphi/StoreReportInDB.res

-123 KB
Binary file not shown.

Delphi/example.dat

24.5 KB
Binary file not shown.

Delphi/uData.dfm

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,8 @@ object DataModule1: TDataModule1
794794
69616C697A65723E0D0A010B0000005400610062006C0065005200650070006F
795795
0072007400}
796796
SortOptions = []
797-
Left = 408
798-
Top = 112
797+
Left = 424
798+
Top = 104
799799
object mdLayoutsLayout: TBlobField
800800
FieldName = 'Layout'
801801
end
@@ -816,10 +816,9 @@ object DataModule1: TDataModule1
816816
ConnectionString = 'XpoProvider=SQLite; Data Source=nwind.db; Mode=ReadOnly'
817817
end
818818
end
819-
object dxSkinController1: TdxSkinController
820-
NativeStyle = False
821-
SkinName = 'WXI'
822-
Left = 304
823-
Top = 104
819+
object ApplicationEvents1: TApplicationEvents
820+
OnException = ApplicationEvents1Exception
821+
Left = 208
822+
Top = 264
824823
end
825824
end

Delphi/uData.pas

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ interface
44

55
uses
66
System.SysUtils, System.Classes, Data.DB, dxmdaset, cxClasses,
7-
dxCore, cxLookAndFeels,
8-
dxSkinsForm, dxBackend, dxBackend.ConnectionString.SQL;
7+
dxCore, cxLookAndFeels, dxMessageDialog,
8+
dxSkinsForm, dxBackend, dxBackend.ConnectionString.SQL, Vcl.AppEvnts;
99

1010
type
1111
TDataModule1 = class(TDataModule)
@@ -15,7 +15,8 @@ TDataModule1 = class(TDataModule)
1515
mdLayoutsName: TWideStringField;
1616
dxBackendDataConnectionManager1: TdxBackendDataConnectionManager;
1717
ReportsNWindConnectionString: TdxBackendDatabaseSQLConnection;
18-
dxSkinController1: TdxSkinController;
18+
ApplicationEvents1: TApplicationEvents;
19+
procedure ApplicationEvents1Exception(Sender: TObject; E: Exception);
1920
procedure DataModuleCreate(Sender: TObject);
2021
procedure DataModuleDestroy(Sender: TObject);
2122
private
@@ -35,6 +36,11 @@ implementation
3536
const
3637
DataFileName = 'data.dat';
3738

39+
procedure TDataModule1.ApplicationEvents1Exception(Sender: TObject; E: Exception);
40+
begin
41+
dxShowMessage(e.Message);
42+
end;
43+
3844
procedure TDataModule1.DataModuleCreate(Sender: TObject);
3945
begin
4046
if FileExists(DataFileName) then

Delphi/uMainForm.dfm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ object MainForm: TMainForm
4646
object gvLayouts: TcxGridDBTableView
4747
DataController.DataSource = DataModule1.dsLayouts
4848
OptionsData.CancelOnExit = False
49-
OptionsData.Deleting = False
50-
OptionsData.DeletingConfirmation = False
51-
OptionsData.Editing = False
52-
OptionsData.Inserting = False
5349
OptionsView.ColumnAutoWidth = True
5450
OptionsView.GroupByBox = False
5551
OptionsView.Indicator = True
@@ -60,6 +56,7 @@ object MainForm: TMainForm
6056
object gvLayoutsLayout: TcxGridDBColumn
6157
Caption = 'Report Layout'
6258
DataBinding.FieldName = 'Layout'
59+
Options.Editing = False
6360
end
6461
object gvLayoutsName: TcxGridDBColumn
6562
Caption = 'Report Name'

Delphi/uMainForm.pas

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
interface
44

5-
uses
6-
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7-
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxClasses, dxReport, cxGraphics,
8-
cxLookAndFeels, cxLookAndFeelPainters, Vcl.Menus, cxButtons,
9-
cxControls, cxStyles, cxCustomData, cxFilter, cxData,
10-
cxDataStorage, cxEdit, cxNavigator, dxDateRanges, dxScrollbarAnnotations,
11-
Data.DB, cxDBData, cxGridLevel, cxGridCustomView, cxGridCustomTableView,
12-
cxGridTableView, cxGridDBTableView, cxGrid, FireDAC.Comp.DataSet,
13-
FireDAC.Comp.Client,
14-
Vcl.StdCtrls, dxmdaset, uData,
15-
dxLayoutControlAdapters, dxLayoutContainer, dxLayoutControl;
5+
uses Vcl.Forms, dxMessageDialog, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, Vcl.Menus,
6+
cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator,
7+
dxDateRanges, dxScrollbarAnnotations, Data.DB, cxDBData,
8+
dxLayoutControlAdapters, dxLayoutContainer, dxReport, cxGridLevel,
9+
cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
10+
cxGridCustomView, cxGrid, Vcl.StdCtrls, cxButtons, System.Classes,
11+
Vcl.Controls, dxLayoutControl;
12+
13+
1614

1715
type
1816
TMainForm = class(TForm)
@@ -37,6 +35,8 @@ TMainForm = class(TForm)
3735
procedure btnNewClick(Sender: TObject);
3836
procedure dxReport1LayoutChanged(ASender: TdxReport);
3937
private
38+
procedure LoadReportNameAndLayout;
39+
procedure SaveReportNameAndLayout;
4040
{ Private declarations }
4141
public
4242
{ Public declarations }
@@ -49,43 +49,61 @@ implementation
4949

5050
{$R *.dfm}
5151

52+
uses uData;
5253

53-
procedure TMainForm.btnNewClick(Sender: TObject);
54-
begin
55-
DataModule1.mdLayouts.Append
56-
end;
57-
58-
procedure TMainForm.btnDesignClick(Sender: TObject);
54+
procedure TMainForm.LoadReportNameAndLayout();
5955
begin
6056
if (DataModule1.mdLayouts.RecordCount = 0) and not (DataModule1.mdLayouts.State = dsInsert) then
6157
begin
62-
ShowMessage('The database is empty');
58+
dxShowMessage('The database is empty');
6359
Exit;
6460
end;
61+
// Load the report name from the database
6562
dxReport1.ReportName := DataModule1.mdLayoutsName.AsString;
63+
// Load the report layout from the database
6664
dxReport1.Layout.Assign(DataModule1.mdLayoutsLayout);
65+
end;
66+
67+
procedure TMainForm.SaveReportNameAndLayout();
68+
begin
69+
// Start editing the active dataset record
70+
DataModule1.mdLayouts.Edit;
71+
// Save the report name
72+
DataModule1.mdLayoutsName.AsString := dxReport1.ReportName;
73+
// Save the report layout
74+
DataModule1.mdLayoutsLayout.Assign(dxReport1.Layout);
75+
// Finish editing and post the modified record to the database
76+
DataModule1.mdLayouts.Post;
77+
end;
78+
79+
80+
// Handle the OnLayoutChanged event raised when a user saves a report layout in the Report Designer
81+
procedure TMainForm.dxReport1LayoutChanged(ASender: TdxReport);
82+
begin
83+
SaveReportNameAndLayout;
84+
end;
85+
86+
// To create a new report layout, create a new dataset record
87+
procedure TMainForm.btnNewClick(Sender: TObject);
88+
begin
89+
DataModule1.mdLayouts.Append;
90+
end;
91+
92+
procedure TMainForm.btnDesignClick(Sender: TObject);
93+
begin
94+
LoadReportNameAndLayout;
6795
dxReport1.ShowDesigner;
6896
end;
6997

7098
procedure TMainForm.btnPreviewClick(Sender: TObject);
7199
begin
72-
if (DataModule1.mdLayoutsName.AsString = '') then
100+
LoadReportNameAndLayout;
101+
if (dxReport1.ReportName = '') then
73102
begin
74-
ShowMessage('The report is not specified');
103+
dxShowMessage('The report is not specified');
75104
Exit;
76105
end;
77-
78-
dxReport1.ReportName := DataModule1.mdLayoutsName.AsString;
79-
dxReport1.Layout.Assign(DataModule1.mdLayoutsLayout);
80106
dxReport1.ShowViewer;
81107
end;
82108

83-
procedure TMainForm.dxReport1LayoutChanged(ASender: TdxReport);
84-
begin
85-
DataModule1.mdLayouts.Edit;
86-
DataModule1.mdLayoutsLayout.Assign(dxReport1.Layout);
87-
DataModule1.mdLayoutsName.AsString := dxReport1.ReportName;
88-
DataModule1.mdLayouts.Post;
89-
end;
90-
91109
end.

0 commit comments

Comments
 (0)