Skip to content

Commit 64bbc10

Browse files
docs: add a kb on how to programmatically generate tables from scratch (#1771)
* docs: add kb on creating tables programmatically * docs: add a kb on how to programmatically generate tables from scratch * Update knowledge-base/create-tables-programmatically.md * Update knowledge-base/create-tables-programmatically.md Co-authored-by: Yordan <[email protected]> * Update knowledge-base/create-tables-programmatically.md Co-authored-by: Yordan <[email protected]> * Update knowledge-base/create-tables-programmatically.md Co-authored-by: Yordan <[email protected]> * Update knowledge-base/create-tables-programmatically.md * Update knowledge-base/create-tables-programmatically.md --------- Co-authored-by: Yordan <[email protected]>
1 parent 4a55850 commit 64bbc10

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
title: How to Create Tables Programmatically in Telerik Reporting
3+
description: Learn how to create tables from scratch at runtime using Telerik Reporting.
4+
type: how-to
5+
page_title: How to Create Tables Programmatically in Telerik Reporting
6+
meta_title: How to Create Tables Programmatically in Telerik Reporting
7+
slug: generating-runtime-tables-telerik-reporting
8+
tags: telerik reporting, runtime report creation, programmatic report, report generation, dynamic table
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td> Product </td>
18+
<td> Progress® Telerik® Reporting </td>
19+
</tr>
20+
<tr>
21+
<td> Version </td>
22+
<td> 19.1.25.521 </td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
This article demonstrates how to create tables programmatically in Telerik Reporting. While this approach offers full control over table structure and data binding, it is recommended to start with a table generated in one of the Telerik Report Designer tools and then modify it programmatically if needed. The Telerik Report Designer handles complex layout and grouping logic, making it easier to maintain and extend reports.
30+
31+
## Prerequisites
32+
33+
Before creating a table programmatically, ensure you have a valid `Telerik.Reporting.Report` object with a predefined structure and a data source ready be set to the table. If not, follow the steps below to create them programmatically.
34+
35+
### 1. Initializing the Report
36+
37+
Create a new report object and configure its basic settings:
38+
39+
````csharp
40+
var report = new Telerik.Reporting.Report();
41+
report.Name = "Report1";
42+
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));
43+
report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
44+
report.Width = Telerik.Reporting.Drawing.Unit.Cm(17D);
45+
````
46+
47+
### 2. Adding Report Sections
48+
49+
Most reports include three main sections:
50+
51+
- `PageHeaderSection`: Appears at the top of each page.
52+
- `DetailSection`: Contains the main content, such as tables or charts.
53+
- `PageFooterSection`: Appears at the bottom of each page.
54+
55+
````csharp
56+
var pageHeaderSection = new Telerik.Reporting.PageHeaderSection();
57+
pageHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D);
58+
pageHeaderSection.Name = "pageHeaderSection";
59+
60+
var detailSection = new Telerik.Reporting.DetailSection();
61+
detailSection.Height = Telerik.Reporting.Drawing.Unit.Cm(5D);
62+
detailSection.Name = "detail";
63+
64+
var pageFooterSection = new Telerik.Reporting.PageFooterSection();
65+
pageFooterSection.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D);
66+
pageFooterSection.Name = "pageFooterSection";
67+
68+
report.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { pageHeaderSection, detailSection, pageFooterSection });
69+
````
70+
71+
### 3. Preparing the Data Source
72+
73+
Telerik Reporting supports various data sources including JSON, CSV, SQL, business objects, and others. For more information, check the article [Data Source Components at a Glance]({%slug telerikreporting/designing-reports/connecting-to-data/data-source-components/overview%}) This example uses an [SqlDataSource component]({%slug telerikreporting/designing-reports/connecting-to-data/data-source-components/sqldatasource-component/overview%}):
74+
75+
````csharp
76+
string selectCommand = "SELECT * FROM production.productphoto;";
77+
string connectionString = "server=localhost\\sqlexpress;database=AdventureWorks2022;trusted_connection=true;";
78+
var sqlDataSource = new Telerik.Reporting.SqlDataSource(connectionString, selectCommand);
79+
sqlDataSource.Name = "sqlDataSource";
80+
sqlDataSource.ProviderName = "System.Data.SqlClient";
81+
````
82+
83+
## Solution
84+
85+
Once the report structure and data source are ready, you can proceed with creating the table.
86+
87+
### 1. Create the Table
88+
89+
Instantiate a `Telerik.Reporting.Table` object, assign the pre-created data source, and add the table to the detail section:
90+
91+
````csharp
92+
var table = new Telerik.Reporting.Table();
93+
table.Name = "table";
94+
table.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Cm(1.5D), Telerik.Reporting.Drawing.Unit.Cm(1.5D));
95+
table.DataSource = sqlDataSource;
96+
detailSection.Items.Add(table);
97+
````
98+
99+
### 2. Add a Detail Row Group
100+
101+
Add a detail row group and a body row.
102+
103+
````csharp
104+
var rowGroup = new Telerik.Reporting.TableGroup();
105+
rowGroup.Name = "detailTableGroup";
106+
rowGroup.Groupings.Add(new Telerik.Reporting.Grouping(null));
107+
table.RowGroups.Add(rowGroup);
108+
table.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Cm(0.609D)));
109+
````
110+
111+
>note Adding a detail row group is mandatory for the table to display data. When using a report designer, this group is created automatically. To mark the group as a detail group programmatically, use a null grouping as shown above.
112+
113+
### 3. Add Columns Dynamically Based on Data
114+
115+
The method for obtaining field names for your table columns depends on the type of data source you are using. In this example, since an `SqlDataSource` is utilized, the code below demonstrates how to extract column names directly from an SQL database.
116+
117+
> If your data source is a CSV, JSON, or another format, adjust the logic to extract field names and create the string collection named `columnNames`. Once you have the field names, the process for dynamically generating table columns remains consistent with the code snippet below.
118+
119+
````csharp
120+
using (var connection = new System.Data.SqlClient.SqlConnection(connectionString))
121+
{
122+
// retrieve the column names from the data source
123+
var command = new System.Data.SqlClient.SqlCommand(selectCommand, connection);
124+
connection.Open();
125+
var reader = command.ExecuteReader();
126+
var dataTable = new System.Data.DataTable();
127+
dataTable.Load(reader);
128+
var columnNames = dataTable.Columns.Cast<System.Data.DataColumn>().Select(c => c.ColumnName);
129+
130+
// for each column name
131+
int colIndex = 0;
132+
foreach (string columnName in columnNames)
133+
{
134+
// add column
135+
var column = new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Cm(2.99D));
136+
table.Body.Columns.Add(column);
137+
138+
// add column header
139+
var headerTextBox = new Telerik.Reporting.TextBox();
140+
headerTextBox.Name = columnName + "TextBoxHeader";
141+
headerTextBox.Value = columnName;
142+
headerTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2.99D), Telerik.Reporting.Drawing.Unit.Cm(0.609D));
143+
144+
var columnGroup = new Telerik.Reporting.TableGroup();
145+
columnGroup.Name = columnName;
146+
columnGroup.ReportItem = headerTextBox;
147+
table.ColumnGroups.Add(columnGroup);
148+
149+
// add column body
150+
var bodyTextBox = new Telerik.Reporting.TextBox();
151+
bodyTextBox.Name = columnName + "TextBoxBody";
152+
bodyTextBox.Value = "= Fields." + columnName;
153+
bodyTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2.99D), Telerik.Reporting.Drawing.Unit.Cm(0.609D));
154+
table.Body.SetCellContent(0, colIndex, bodyTextBox);
155+
156+
colIndex++;
157+
}
158+
}
159+
````
160+
161+
## Demo
162+
163+
You can find a sample console application that demonstrates the above approach at [reporting-samples/ProgrammaticTableGeneration](https://github.com/telerik/reporting-samples/tree/master/ProgrammaticTableGeneration).

0 commit comments

Comments
 (0)