-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReportCreator.cs
175 lines (144 loc) · 6.9 KB
/
ReportCreator.cs
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
#region usings
using DevExpress.DataAccess.Sql;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using System.Drawing;
#endregion
namespace RuntimeSqlDataSourceReportSample
{
class ReportCreator
{
#region CreateReport
public static XtraReport CreateReport(object dataSource)
{
SqlDataSource ds = dataSource as SqlDataSource;
if (ds == null) return new XtraReport();
// Create an empty report.
XtraReport report = new XtraReport();
// Bind the report to a data source.
report.DataSource = ds;
report.DataMember = ds.Queries[0].Name;
// Create a master part.
CreateReportHeader(report, "Products by Categories");
CreateDetail(report);
// Create a detail part.
CreateDetailReport(report, ds.Queries[0].Name + "." + ds.Relations[0].Name);
return report;
}
#endregion
#region CreateMasterReport
private static void CreateReportHeader(XtraReport report, string caption)
{
// Create a report title.
XRLabel label = new XRLabel();
label.Font = new Font("Tahoma", 12, FontStyle.Bold);
label.Text = caption;
label.WidthF = 300F;
// Create a report header and add the title to it.
ReportHeaderBand reportHeader = new ReportHeaderBand();
report.Bands.Add(reportHeader);
reportHeader.Controls.Add(label);
reportHeader.HeightF = label.HeightF;
}
private static void CreateDetail(XtraReport report)
{
// Create a new label bound to the CategoryName data field.
XRLabel labelDetail = new XRLabel();
labelDetail.Font = new Font("Tahoma", 10, FontStyle.Bold);
labelDetail.WidthF = 300F;
// Bind the label to the CategoryName data field.
labelDetail.ExpressionBindings.Add(
new ExpressionBinding("BeforePrint", "Text", "'Category: ' + [CategoryName]"));
// Create a detail band and display the category name in it.
DetailBand detailBand = new DetailBand();
detailBand.Height = labelDetail.Height;
detailBand.KeepTogetherWithDetailReports = true;
report.Bands.Add(detailBand);
labelDetail.TopF = detailBand.LocationFloat.Y + 20F;
detailBand.Controls.Add(labelDetail);
}
#endregion
#region CreateDetailReport
private static void CreateDetailReport(XtraReport report, string dataMember)
{
// Create a detail report band and bind it to data.
DetailReportBand detailReportBand = new DetailReportBand();
report.Bands.Add(detailReportBand);
detailReportBand.DataSource = report.DataSource;
detailReportBand.DataMember = dataMember;
// Add a header to the detail report.
ReportHeaderBand detailReportHeader = new ReportHeaderBand();
detailReportBand.Bands.Add(detailReportHeader);
XRTable tableHeader = new XRTable();
tableHeader.BeginInit();
tableHeader.Rows.Add(new XRTableRow());
tableHeader.Borders = BorderSide.All;
tableHeader.BorderColor = Color.DarkGray;
tableHeader.Font = new Font("Tahoma", 10, FontStyle.Bold);
tableHeader.Padding = 10;
tableHeader.TextAlignment = TextAlignment.MiddleLeft;
XRTableCell cellHeader1 = new XRTableCell();
cellHeader1.Text = "Product Name";
XRTableCell cellHeader2 = new XRTableCell();
cellHeader2.Text = "Unit Price";
cellHeader2.TextAlignment = TextAlignment.MiddleRight;
tableHeader.Rows[0].Cells.AddRange(new XRTableCell[] { cellHeader1, cellHeader2 });
detailReportHeader.Height = tableHeader.Height;
detailReportHeader.Controls.Add(tableHeader);
// Adjust the table width.
tableHeader.BeforePrint += tableHeader_BeforePrint;
tableHeader.EndInit();
// Create a detail band.
XRTable tableDetail = new XRTable();
tableDetail.BeginInit();
tableDetail.Rows.Add(new XRTableRow());
tableDetail.Borders = BorderSide.Left | BorderSide.Right | BorderSide.Bottom;
tableDetail.BorderColor = Color.DarkGray;
tableDetail.Font = new Font("Tahoma", 10);
tableDetail.Padding = 10;
tableDetail.TextAlignment = TextAlignment.MiddleLeft;
XRTableCell cellDetail1 = new XRTableCell();
XRTableCell cellDetail2 = new XRTableCell();
cellDetail2.TextAlignment = TextAlignment.MiddleRight;
cellDetail1.ExpressionBindings.Add(
new ExpressionBinding("BeforePrint", "Text", "[ProductName]"));
cellDetail2.ExpressionBindings.Add(
new ExpressionBinding("BeforePrint", "Text",
"FormatString('{0:$0.00}', [UnitPrice])"));
tableDetail.Rows[0].Cells.AddRange(new XRTableCell[] { cellDetail1, cellDetail2 });
DetailBand detailBand = new DetailBand();
detailBand.Height = tableDetail.Height;
detailReportBand.Bands.Add(detailBand);
detailBand.Controls.Add(tableDetail);
// Adjust the table width.
tableDetail.BeforePrint += tableDetail_BeforePrint;
tableDetail.EndInit();
// Create and assign different odd and even styles.
XRControlStyle oddStyle = new XRControlStyle();
XRControlStyle evenStyle = new XRControlStyle();
oddStyle.BackColor = Color.WhiteSmoke;
oddStyle.StyleUsing.UseBackColor = true;
oddStyle.Name = "OddStyle";
evenStyle.BackColor = Color.White;
evenStyle.StyleUsing.UseBackColor = true;
evenStyle.Name = "EvenStyle";
report.StyleSheet.AddRange(new XRControlStyle[] { oddStyle, evenStyle });
tableDetail.OddStyleName = "OddStyle";
tableDetail.EvenStyleName = "EvenStyle";
}
private static void AdjustTableWidth(XRTable table)
{
XtraReport report = table.RootReport;
table.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;
}
static void tableHeader_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
AdjustTableWidth(sender as XRTable);
}
static void tableDetail_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
AdjustTableWidth(sender as XRTable);
}
#endregion
}
}