Skip to content

Commit 2b8ee36

Browse files
authored
Merge pull request #321 from telerik/petodoro/table-generation
chore: add programmatic table generation app
2 parents 728027b + cd0363d commit 2b8ee36

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
namespace ProgrammaticTableGeneration
2+
{
3+
internal class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
// make sure to adjust the connection string and select command to match your database
8+
string selectCommand = "SELECT * FROM production.productphoto;";
9+
string connectionString = "server=localhost\\sqlexpress;database=AdventureWorks2022;trusted_connection=true;";
10+
11+
// create the report
12+
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
13+
report.Name = "Report1";
14+
report.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));
15+
report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
16+
report.Width = Telerik.Reporting.Drawing.Unit.Cm(17D);
17+
18+
// add the main sections to the report
19+
var pageHeaderSection = new Telerik.Reporting.PageHeaderSection();
20+
pageHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D);
21+
pageHeaderSection.Name = "pageHeaderSection";
22+
23+
var detailSection = new Telerik.Reporting.DetailSection();
24+
detailSection.Height = Telerik.Reporting.Drawing.Unit.Cm(5D);
25+
detailSection.Name = "detail";
26+
27+
var pageFooterSection = new Telerik.Reporting.PageFooterSection();
28+
pageFooterSection.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D);
29+
pageFooterSection.Name = "pageFooterSection";
30+
31+
report.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { pageHeaderSection, detailSection, pageFooterSection });
32+
33+
// prepare the data source for the table
34+
var sqlDataSource = new Telerik.Reporting.SqlDataSource(connectionString, selectCommand);
35+
sqlDataSource.Name = "sqlDataSource";
36+
sqlDataSource.ProviderName = "System.Data.SqlClient";
37+
38+
// create the table and set its data source
39+
var table = new Telerik.Reporting.Table();
40+
table.Name = "table";
41+
table.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Cm(1.5D), Telerik.Reporting.Drawing.Unit.Cm(1.5D));
42+
table.DataSource = sqlDataSource;
43+
detailSection.Items.Add(table);
44+
45+
// add a detail row group to the table
46+
var rowGroup = new Telerik.Reporting.TableGroup();
47+
rowGroup.Name = "detailTableGroup";
48+
rowGroup.Groupings.Add(new Telerik.Reporting.Grouping(null));
49+
table.RowGroups.Add(rowGroup);
50+
table.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Cm(0.609D)));
51+
52+
using (var connection = new System.Data.SqlClient.SqlConnection(connectionString))
53+
{
54+
// retrieve the column names from the data source
55+
var command = new System.Data.SqlClient.SqlCommand(selectCommand, connection);
56+
connection.Open();
57+
var reader = command.ExecuteReader();
58+
var dataTable = new System.Data.DataTable();
59+
dataTable.Load(reader);
60+
var columnNames = dataTable.Columns.Cast<System.Data.DataColumn>().Select(c => c.ColumnName);
61+
62+
// for each column name
63+
int colIndex = 0;
64+
foreach (string columnName in columnNames)
65+
{
66+
// add column
67+
var column = new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Cm(2.99D));
68+
table.Body.Columns.Add(column);
69+
70+
// add column header
71+
var headerTextBox = new Telerik.Reporting.TextBox();
72+
headerTextBox.Name = columnName + "TextBoxHeader";
73+
headerTextBox.Value = columnName;
74+
headerTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2.99D), Telerik.Reporting.Drawing.Unit.Cm(0.609D));
75+
76+
var columnGroup = new Telerik.Reporting.TableGroup();
77+
columnGroup.Name = columnName;
78+
columnGroup.ReportItem = headerTextBox;
79+
table.ColumnGroups.Add(columnGroup);
80+
81+
// add column body
82+
var bodyTextBox = new Telerik.Reporting.TextBox();
83+
bodyTextBox.Name = columnName + "TextBoxBody";
84+
bodyTextBox.Value = "= Fields." + columnName;
85+
bodyTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2.99D), Telerik.Reporting.Drawing.Unit.Cm(0.609D));
86+
table.Body.SetCellContent(0, colIndex, bodyTextBox);
87+
88+
colIndex++;
89+
}
90+
}
91+
92+
// package the programmatically created report into a .trdp file
93+
var reportPackager = new Telerik.Reporting.ReportPackager();
94+
using (var targetStream = System.IO.File.Create("PackageReport1.trdp"))
95+
{
96+
reportPackager.Package(report, targetStream);
97+
}
98+
System.Console.WriteLine("Report created successfully at: " + Path.GetFullPath("PackageReport1.trdp"));
99+
}
100+
}
101+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Telerik.Reporting" Version="19.1.25.521" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36202.13 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgrammaticTableGeneration", "ProgrammaticTableGeneration.csproj", "{4858440A-9036-4274-9D43-F290F9E939A2}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4858440A-9036-4274-9D43-F290F9E939A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4858440A-9036-4274-9D43-F290F9E939A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4858440A-9036-4274-9D43-F290F9E939A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{4858440A-9036-4274-9D43-F290F9E939A2}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {568A4414-022A-4637-B4A7-A4DB5B2C2730}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)