-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseReport.cs
More file actions
251 lines (233 loc) · 12.6 KB
/
BaseReport.cs
File metadata and controls
251 lines (233 loc) · 12.6 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Data;
using System.Data.SqlClient;
using Telerik.Reporting;
using Telerik.Reporting.Drawing;
using System.Collections.Generic;
using System.Linq;
using TextBox = Telerik.Reporting.TextBox;
namespace TelerikReportWebAPI.Reports
{
interface IBaseReport
{
void InitializeReport(string connectionString, string query);
void AddAdditionalColumns(DataTable dataTable);
}
public class BaseReport : Telerik.Reporting.Report, IBaseReport
{
public void InitializeComponent()
{
// Your existing InitializeComponent code here
Telerik.Reporting.TableGroup tableGroup1 = new Telerik.Reporting.TableGroup();
Telerik.Reporting.TableGroup tableGroup2 = new Telerik.Reporting.TableGroup();
Telerik.Reporting.Drawing.StyleRule styleRule1 = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.StyleRule styleRule2 = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.StyleRule styleRule3 = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.DescendantSelector descendantSelector1 = new Telerik.Reporting.Drawing.DescendantSelector();
Telerik.Reporting.Drawing.StyleRule styleRule4 = new Telerik.Reporting.Drawing.StyleRule();
Telerik.Reporting.Drawing.DescendantSelector descendantSelector2 = new Telerik.Reporting.Drawing.DescendantSelector();
this.textBox1 = new Telerik.Reporting.TextBox();
this.detailSection1 = new Telerik.Reporting.DetailSection();
this.table1 = new Telerik.Reporting.Table();
this.textBox2 = new Telerik.Reporting.TextBox();
this.pageHeaderSection1 = new Telerik.Reporting.PageHeaderSection();
this.pageFooterSection1 = new Telerik.Reporting.PageFooterSection();
((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
//
// textBox1
//
this.textBox1.Name = "textBox1";
this.textBox1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2D), Telerik.Reporting.Drawing.Unit.Cm(0.5D));
this.textBox1.Style.BorderWidth.Default = Telerik.Reporting.Drawing.Unit.Pixel(0D);
this.textBox1.Style.Visible = false;
this.textBox1.StyleName = "Normal.TableHeader";
this.textBox1.Value = "";
//
// detailSection1
//
this.detailSection1.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D);
this.detailSection1.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
this.table1});
this.detailSection1.Name = "detailSection1";
//
// table1
//
this.table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Cm(2D)));
this.table1.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Cm(0.5D)));
this.table1.Body.SetCellContent(0, 0, this.textBox2);
tableGroup1.Name = "countryId";
tableGroup1.ReportItem = this.textBox1;
this.table1.ColumnGroups.Add(tableGroup1);
this.table1.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
this.textBox2,
this.textBox1});
this.table1.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Cm(0D), Telerik.Reporting.Drawing.Unit.Cm(0D));
this.table1.Name = "table1";
tableGroup2.Groupings.Add(new Telerik.Reporting.Grouping(null));
tableGroup2.Name = "detail";
this.table1.RowGroups.Add(tableGroup2);
this.table1.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2D), Telerik.Reporting.Drawing.Unit.Cm(1D));
this.table1.Style.BorderWidth.Default = Telerik.Reporting.Drawing.Unit.Pixel(0D);
this.table1.StyleName = "Normal.TableNormal";
//
// textBox2
//
this.textBox2.Name = "textBox2";
this.textBox2.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2D), Telerik.Reporting.Drawing.Unit.Cm(0.5D));
this.textBox2.Style.BorderWidth.Default = Telerik.Reporting.Drawing.Unit.Pixel(0D);
this.textBox2.Style.Visible = false;
this.textBox2.StyleName = "Normal.TableBody";
this.textBox2.Value = "";
//
// pageHeaderSection1
//
this.pageHeaderSection1.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D);
this.pageHeaderSection1.Name = "pageHeaderSection1";
//
// pageFooterSection1
//
this.pageFooterSection1.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D);
this.pageFooterSection1.Name = "pageFooterSection1";
//
// Report2
//
this.Items.AddRange(new Telerik.Reporting.ReportItemBase[] {
this.detailSection1,
this.pageHeaderSection1,
this.pageFooterSection1});
this.Name = "Report2";
this.PageSettings.Margins = new Telerik.Reporting.Drawing.MarginsU(Telerik.Reporting.Drawing.Unit.Mm(20D), Telerik.Reporting.Drawing.Unit.Mm(20D), Telerik.Reporting.Drawing.Unit.Mm(20D), Telerik.Reporting.Drawing.Unit.Mm(20D));
this.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
styleRule1.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.TextItemBase)),
new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.HtmlTextBox))});
styleRule1.Style.Padding.Left = Telerik.Reporting.Drawing.Unit.Point(2D);
styleRule1.Style.Padding.Right = Telerik.Reporting.Drawing.Unit.Point(2D);
styleRule2.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.StyleSelector(typeof(Telerik.Reporting.Table), "Normal.TableNormal")});
styleRule2.Style.BorderColor.Default = System.Drawing.Color.Black;
styleRule2.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.Solid;
styleRule2.Style.BorderWidth.Default = Telerik.Reporting.Drawing.Unit.Pixel(1D);
styleRule2.Style.Color = System.Drawing.Color.Black;
styleRule2.Style.Font.Name = "Tahoma";
styleRule2.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(9D);
descendantSelector1.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.Table)),
new Telerik.Reporting.Drawing.StyleSelector(typeof(Telerik.Reporting.ReportItem), "Normal.TableBody")});
styleRule3.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
descendantSelector1});
styleRule3.Style.BorderColor.Default = System.Drawing.Color.Black;
styleRule3.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.Solid;
styleRule3.Style.BorderWidth.Default = Telerik.Reporting.Drawing.Unit.Pixel(1D);
styleRule3.Style.Font.Name = "Tahoma";
styleRule3.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(9D);
descendantSelector2.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
new Telerik.Reporting.Drawing.TypeSelector(typeof(Telerik.Reporting.Table)),
new Telerik.Reporting.Drawing.StyleSelector(typeof(Telerik.Reporting.ReportItem), "Normal.TableHeader")});
styleRule4.Selectors.AddRange(new Telerik.Reporting.Drawing.ISelector[] {
descendantSelector2});
styleRule4.Style.BorderColor.Default = System.Drawing.Color.Black;
styleRule4.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.Solid;
styleRule4.Style.BorderWidth.Default = Telerik.Reporting.Drawing.Unit.Pixel(1D);
styleRule4.Style.Font.Name = "Tahoma";
styleRule4.Style.Font.Size = Telerik.Reporting.Drawing.Unit.Point(10D);
styleRule4.Style.VerticalAlign = Telerik.Reporting.Drawing.VerticalAlign.Middle;
this.StyleSheet.AddRange(new Telerik.Reporting.Drawing.StyleRule[] {
styleRule1,
styleRule2,
styleRule3,
styleRule4});
this.Width = Telerik.Reporting.Drawing.Unit.Cm(17D);
((System.ComponentModel.ISupportInitialize)(this)).EndInit();
// Make sure to initialize this.reportTable
}
public virtual void InitializeReport(string connectionString, string query)
{
var data = GetData(connectionString, query);
Console.WriteLine(data.ToString());
this.table1.DataSource = data;
AddDynamicColumns(data);
}
public virtual DataTable GetData(string connectionString, string query)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();
try
{
connection.Open();
adapter.Fill(dataTable);
AddAdditionalColumns(dataTable);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
return dataTable;
}
}
public virtual void AddAdditionalColumns(DataTable dataTable)
{
// Override this method in derived classes to add specific columns
}
public virtual void AddDynamicColumns(DataTable dataTable)
{
int existingColumns = this.table1.Body.Columns.Count;
for (int i = existingColumns; i < dataTable.Columns.Count; i++)
{
string columnName = dataTable.Columns[i].ColumnName;
this.table1.Body.Columns.Add(new TableBodyColumn(Unit.Cm(2)));
var headerTextBox = new TextBox
{
Name = $"textBox{i + 1}",
Size = new SizeU(Unit.Cm(2), Unit.Cm(0.5)),
StyleName = "Normal.TableHeader",
Value = columnName
};
var tableGroup = new TableGroup();
tableGroup.ReportItem = headerTextBox;
this.table1.ColumnGroups.Add(tableGroup);
var bodyTextBox = new TextBox
{
Name = $"textBox{i + existingColumns + 1}",
Size = new SizeU(Unit.Cm(2), Unit.Cm(0.5)),
StyleName = "Normal.TableBody",
Value = $"= Fields.{columnName}"
};
this.table1.Body.SetCellContent(0, i, bodyTextBox);
}
this.table1.Size = new SizeU(Unit.Cm(2 * dataTable.Columns.Count), this.table1.Size.Height);
}
public void AddColumnsToDataTable(DataTable dataTable, List<(string Name, Type Type, Func<DataRow, int, object> ValueGenerator)> columnsToAdd)
{
foreach (var (name, type, valueGenerator) in columnsToAdd)
{
dataTable.Columns.Add(name, type);
}
for (int i = 0; i < dataTable.Rows.Count; i++)
{
DataRow row = dataTable.Rows[i];
foreach (var (name, _, valueGenerator) in columnsToAdd)
{
row[name] = valueGenerator(row, i);
}
}
}
public string GenerateRandomString(int length, Random random)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private Telerik.Reporting.DetailSection detailSection1;
private Telerik.Reporting.Table table1;
private Telerik.Reporting.TextBox textBox2;
private Telerik.Reporting.TextBox textBox1;
private Telerik.Reporting.PageHeaderSection pageHeaderSection1;
private Telerik.Reporting.PageFooterSection pageFooterSection1;
// InitializeComponent method should be implemented in derived classes
///public abstract void InitializeComponent();
}
}