-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassicReport.cs
223 lines (207 loc) · 4.8 KB
/
ClassicReport.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using System.Reflection;
using System.Windows.Forms;
using JasonJimenez.ClassicReport.Common;
namespace JasonJimenez.ClassicReport
{
public class ClassicReport : Component
{
string _template;
object datasource;
object target;
IEnumerator enumerator;
ClassicReportDocument _document;
/// <summary>
/// Occurs when the current row has changed.
/// </summary>
public event ClassicReportDataHandler RowChanged;
/// <summary>
/// Occurs when the value of a report field needs to be updated.
/// </summary>
public event ClassicReportDataHandler ValueOverride;
public ClassicReport()
{
}
[Browsable(false)]
public object DataSource
{
get { return datasource; }
set { datasource = value; }
}
public string DocumentName
{
get
{
if (_template != null || _template.Trim() != "")
{
ReportModel model = new ReportModel();
model.LoadSchema(_template);
return model.DocumentName;
}
return "";
}
}
[Browsable(false)]
public ClassicReportDocument Document
{
get { return _document; }
}
[Editor(typeof(TemplateEditor),typeof(UITypeEditor))]
public string Template
{
get { return _template; }
set { _template = value; }
}
public void Execute()
{
if (DataSource != null)
{
ReportComposer composer = new ReportComposer(_template);
composer.InitData += new ReportComposerEventHandler(OnInitData);
composer.ReadData += new ReportComposerEventHandler(OnReadData);
composer.Execute();
_document = composer.Document;
}
else
{
_document = null;
}
}
#region ReportComposer event handlers
void OnInitData(object sender, ReportComposerEventArgs e)
{
object _ds;
if (DataSource is BindingSource)
_ds = ((BindingSource)DataSource).DataSource;
else
_ds = DataSource;
// initialize the record count and datasource here
if (e.Bof)
{
e.Bof = false;
if (_ds is System.Data.DataSet )
{
e.Count = ((System.Data.DataSet)_ds).Tables[0].Rows.Count;
if (e.Count == 0)
{
e.Eof = true;
return;
}
}
else if (_ds is System.Data.DataTable)
{
e.Count = ((System.Data.DataTable)_ds).Rows.Count;
if (e.Count == 0)
{
e.Eof = true;
return;
}
}
else if (_ds is IEnumerable)
{
e.Count = int.MaxValue;
enumerator = ((IEnumerable)_ds).GetEnumerator();
}
else if (_ds is IList)
{
e.Count = ((IList)_ds).Count;
if (e.Count == 0)
{
e.Eof = true;
return;
}
}
else if (_ds != null)
{
e.Count = 1;
}
else
{
e.Eof = true;
return;
}
}
// process a row here.
if (_ds is System.Data.DataSet)
{
target = ((System.Data.DataSet)_ds).Tables[0].Rows[e.Index];
OnRowChanged(new ClassicReportDataEventArgs(target, e));
}
else if (_ds is System.Data.DataTable)
{
target = ((System.Data.DataTable)DataSource).Rows[e.Index];
OnRowChanged(new ClassicReportDataEventArgs(target, e));
}
else if (_ds is IEnumerable)
{
if (enumerator.MoveNext())
{
target = enumerator.Current;
OnRowChanged(new ClassicReportDataEventArgs(target, e));
}
else
e.Eof = true;
}
else if (_ds is IList)
{
target = ((IList)_ds)[e.Index];
OnRowChanged(new ClassicReportDataEventArgs(target, e));
}
else
{
target = _ds;
}
}
void OnReadData(object sender, ReportComposerEventArgs e)
{
// let the client process the data first
ClassicReportDataEventArgs args = new ClassicReportDataEventArgs(target, e);
if (ValueOverride != null)
{
ValueOverride(this, args);
if (args.Handled)
{
e.Value = args.Value;
return;
}
}
// the client did not process the data
object _ds;
if (DataSource is BindingSource)
_ds = ((BindingSource)DataSource).DataSource;
else
_ds = DataSource;
if (_ds is System.Data.DataSet || _ds is System.Data.DataTable)
{
System.Data.DataRow row = (System.Data.DataRow)target;
e.Value = row[e.ColumnName];
}
else
{
PropertyInfo property = target.GetType().GetProperty(e.ColumnName);
if (property != null)
e.Value = property.GetValue(target, null);
else
e.Value = null;
}
}
#endregion
void OnRowChanged(ClassicReportDataEventArgs e)
{
if (RowChanged != null)
{
RowChanged(this, e);
}
}
void OnValueOverride(ClassicReportDataEventArgs e)
{
if (ValueOverride != null)
{
ValueOverride(this, e);
}
}
}
}