-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddAnyNoColumnToReport.cs
More file actions
121 lines (107 loc) · 4.43 KB
/
AddAnyNoColumnToReport.cs
File metadata and controls
121 lines (107 loc) · 4.43 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
using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Telerik.Reporting;
using Telerik.Reporting.Drawing;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using TextBox = Telerik.Reporting.TextBox;
namespace TelerikReportWebAPI.Reports
{
/// <summary>
/// Summary description for Report1.
/// </summary>
public partial class Report1 : Telerik.Reporting.Report
{
public Report1()
{
InitializeComponent();
var data = GetData();
this.table1.DataSource = data;
AddDynamicColumns(data);
}
private 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);
}
private DataTable GetData()
{
string connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=countrydb;Integrated Security=True";
string query = "SELECT CountryId, CountryName, Population, Area FROM Countries";
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);
var random = new Random();
var columnsToAdd = new List<(string Name, Type Type, Func<DataRow, int, object> ValueGenerator)>
{
("Password", typeof(string), (DataRow row, int index) => GenerateRandomString(8, random)),
("Gratitude", typeof(string), (DataRow row, int index) => $"Thank God {index + 1}"),
("Number", typeof(int), (DataRow row, int index) => random.Next(1, 101))
};
AddColumnsToDataTable(dataTable, columnsToAdd);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
return dataTable;
}
}
private 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);
}
}
}
private static 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());
}
}
}