-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportGenerationHelpers.cs
More file actions
203 lines (172 loc) · 8.88 KB
/
Copy pathReportGenerationHelpers.cs
File metadata and controls
203 lines (172 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System.Drawing;
using System.Text.Json;
using System.Text.Json.Serialization;
using DevExpress.DataAccess;
using DevExpress.DataAccess.DataFederation;
using DevExpress.DataAccess.ObjectBinding;
using DevExpress.Drawing;
using DevExpress.Drawing.Printing;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using GridMasterDetailExport.Data;
namespace GridMasterDetailExport.Reports {
public static class ReportGenerationHelpers {
public static XtraReport GetReportFromDxGrid(string[] masterColumnNames, string[] detailColumnNames, Dictionary<string, bool> rowExpandedStates) {
XtraReport report = new XtraReport() {
ReportUnit = ReportUnit.HundredthsOfAnInch,
PaperKind = DXPaperKind.Letter,
Margins = new DXMargins(100, 100, 100, 100)
};
#region Styles
var tableHeaderStyle = new XRControlStyle {
Name = "tableHeaderStyle",
TextAlignment = TextAlignment.MiddleCenter,
BackColor = Color.AliceBlue,
};
report.StyleSheet.Add(tableHeaderStyle);
var tableRowStyle = new XRControlStyle() {
Name = "tableRowStyle",
TextAlignment = TextAlignment.MiddleCenter
};
report.StyleSheet.Add(tableRowStyle);
#endregion
#region DataSource
var masterDataSource = new ObjectDataSource() {
Name = "Users",
DataSource = typeof(DataProvider),
DataMember = nameof(DataProvider.GetUsers)
};
masterDataSource.RebuildResultSchema();
var detailDataSource = new ObjectDataSource() {
Name = "Orders",
DataSource = typeof(DataProvider),
DataMember = nameof(DataProvider.GetOrders)
};
detailDataSource.RebuildResultSchema();
var fds = ReportGenerationHelpers.CreateFederationDataSource(masterDataSource, detailDataSource, nameof(User.UserID), nameof(Order.UserID), rowExpandedStates);
#endregion
float contentWidth = report.PageWidthF - report.Margins.Left - report.Margins.Right;
report.DataSource = fds;
report.DataMember = masterDataSource.Name;
report.AddTableHeaderBand(new RectangleF(0f, 0f, contentWidth, 25f), tableHeaderStyle.Name, masterColumnNames);
report.AddTableRowBand(new RectangleF(0f, 0f, contentWidth, 25f), tableRowStyle.Name, masterColumnNames, true);
report.AddDetailReport(
$"{masterDataSource.Name}.{masterDataSource.Name}{detailDataSource.Name}",
detailColumnNames,
new RectangleF(20f, 20f, contentWidth - 20f, 25f),
tableHeaderStyle.Name,
tableRowStyle.Name);
return report;
}
static FederationDataSource CreateFederationDataSource(DataComponentBase masterDataSource, DataComponentBase detailDataSource, string parentKeyColumn, string nestedKeyColumn, Dictionary<string, bool> expandedStates) {
ObjectDataSource expandedStateDataSource = new ObjectDataSource() {
Name = "ExpandedState",
DataSource = typeof(ExpandedState),
DataMember = nameof(ExpandedState.LoadExpandedStates)
};
expandedStateDataSource.Parameters.Add(new Parameter(
"json",
typeof(string),
JsonSerializer.Serialize(expandedStates.Select((i) => new ExpandedState() { RowKey = i.Key, IsExpanded = i.Value }))));
Source masterSource = new Source(masterDataSource.Name, masterDataSource);
Source detailSource = new Source(detailDataSource.Name, detailDataSource);
Source expandedStatesSource = new Source("ExpandedState", expandedStateDataSource);
var masterQuery = masterSource.From()
.SelectAll()
.InnerJoin(expandedStatesSource, $"[{masterSource.Name}.{parentKeyColumn}] = [{expandedStatesSource.Name}].[{nameof(ExpandedState.RowKey)}]")
.Select(nameof(ExpandedState.IsExpanded))
.Build(masterSource.Name);
var detailQuery = detailSource.From()
.SelectAll()
.Build(detailSource.Name);
var federationDataSource = new FederationDataSource();
federationDataSource.Queries.AddRange([masterQuery, detailQuery]);
var relation = new FederationMasterDetailInfo(masterSource.Name, detailSource.Name, new FederationRelationColumnInfo(parentKeyColumn, nestedKeyColumn));
federationDataSource.Relations.Add(relation);
federationDataSource.RebuildResultSchema();
return federationDataSource;
}
public static void AddTableHeaderBand(this XtraReportBase report, RectangleF rect, string styleName, string[] columnCaptions) {
var headerBand = new GroupHeaderBand() {
HeightF = rect.Height,
RepeatEveryPage = true,
KeepTogether = true,
GroupUnion = GroupUnion.WithFirstDetail
};
report.Bands.Add(headerBand);
headerBand.AddXRTable(columnCaptions, rect, styleName,
(table) => {
table.Borders = BorderSide.All;
},
(cell, caption) => {
cell.Text = caption;
});
}
public static void AddTableRowBand(this XtraReportBase report, RectangleF rect, string styleName, string[] fieldNames, bool isExpandable = false) {
var rowBand = new DetailBand() {
HeightF = rect.Height,
KeepTogether = true,
KeepTogetherWithDetailReports = true
};
report.Bands.Add(rowBand);
rowBand.AddXRTable(fieldNames, rect, styleName,
(table) => {
if(isExpandable) {
table.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Borders", $"IIf([DataSource.IsFirstRow] || !PrevRowColumnValue('{nameof(ExpandedState.IsExpanded)}'),'Left,Right,Bottom', 'All')\r\n"));
}
else {
table.Borders = BorderSide.Left | BorderSide.Right | BorderSide.Bottom;
}
},
(cell, fieldName) => {
cell.ExpressionBindings.Add(
new("BeforePrint", "Text", $"[{fieldName}]"));
});
}
public static void AddEmptyTableFooterBand(this XtraReportBase report, float height) {
var footerBand = new GroupFooterBand() {
HeightF = height,
GroupUnion = GroupFooterUnion.WithLastDetail
};
report.Bands.Add(footerBand);
footerBand.Controls.Add(new XRPanel() { HeightF = height });
}
public static void AddDetailReport(this XtraReportBase report, string dataMember, string[] columns, RectangleF rect, string headerStyleName, string rowStyleName) {
var detailReportBand = new DetailReportBand() {
DataSource = report.DataSource,
DataMember = dataMember
};
report.Bands.Add(detailReportBand);
detailReportBand.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Visible", $"[{nameof(ReportGenerationHelpers.ExpandedState.IsExpanded)}]"));
detailReportBand.AddTableHeaderBand(rect, headerStyleName, columns);
detailReportBand.AddTableRowBand(new RectangleF(rect.X, 0f, rect.Width, rect.Height), rowStyleName, columns);
detailReportBand.AddEmptyTableFooterBand(rect.Height);
}
private static void AddXRTable<T>(this Band band, T[] columns, RectangleF rect, string styleName, Action<XRTable> tableInitializer, Action<XRTableCell, T> cellInitializer) {
var table = new XRTable();
band.Controls.Add(table);
table.BeginInit();
table.LocationF = rect.Location;
table.SizeF = rect.Size;
table.StyleName = styleName;
tableInitializer(table);
var row = new XRTableRow();
table.Rows.Add(row);
foreach(T column in columns) {
XRTableCell cell = new XRTableCell();
row.Cells.Add(cell);
cellInitializer(cell, column);
}
table.EndInit();
}
public class ExpandedState {
[JsonRequired]
public string RowKey { get; set; } = "";
[JsonRequired]
public bool IsExpanded { get; set; }
public static IEnumerable<ExpandedState>? LoadExpandedStates(string json) {
return JsonSerializer.Deserialize<List<ExpandedState>>(json);
}
}
}
}