-
Notifications
You must be signed in to change notification settings - Fork 28
docs: add a kb on how to programmatically generate tables from scratch #1771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
petar-i-todorov
merged 8 commits into
master
from
petodoro/kb-create-tables-programmatically
Aug 6, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
59e0881
docs: add kb on creating tables programmatically
petar-i-todorov 5feca2d
docs: add a kb on how to programmatically generate tables from scratch
petar-i-todorov 07e84dd
Update knowledge-base/create-tables-programmatically.md
petar-i-todorov 90b3a3c
Update knowledge-base/create-tables-programmatically.md
petar-i-todorov d6ad9e7
Update knowledge-base/create-tables-programmatically.md
petar-i-todorov ea861c8
Update knowledge-base/create-tables-programmatically.md
petar-i-todorov 8ccac0d
Update knowledge-base/create-tables-programmatically.md
petar-i-todorov 5cac22b
Update knowledge-base/create-tables-programmatically.md
petar-i-todorov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
--- | ||
title: How to Create Tables Programmatically in Telerik Reporting | ||
description: Learn how to create tables from scratch at runtime using Telerik Reporting. | ||
type: how-to | ||
page_title: How to Create Tables Programmatically in Telerik Reporting | ||
meta_title: How to Create Tables Programmatically in Telerik Reporting | ||
slug: generating-runtime-tables-telerik-reporting | ||
tags: telerik reporting, runtime report creation, programmatic report, report generation, dynamic table | ||
res_type: kb | ||
--- | ||
|
||
## Environment | ||
|
||
<table> | ||
<tbody> | ||
<tr> | ||
<td> Product </td> | ||
<td> Progress® Telerik® Reporting </td> | ||
</tr> | ||
<tr> | ||
<td> Version </td> | ||
<td> 19.1.25.521 </td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
## Description | ||
|
||
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. | ||
|
||
## Prerequisites | ||
|
||
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. | ||
|
||
### 1. Initializing the Report | ||
|
||
Create a new report object and configure its basic settings: | ||
|
||
````csharp | ||
var report = new Telerik.Reporting.Report(); | ||
report.Name = "Report1"; | ||
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)); | ||
report.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4; | ||
report.Width = Telerik.Reporting.Drawing.Unit.Cm(17D); | ||
```` | ||
|
||
### 2. Adding Report Sections | ||
|
||
Most reports include three main sections: | ||
|
||
- `PageHeaderSection`: Appears at the top of each page. | ||
- `DetailSection`: Contains the main content, such as tables or charts. | ||
- `PageFooterSection`: Appears at the bottom of each page. | ||
|
||
````csharp | ||
var pageHeaderSection = new Telerik.Reporting.PageHeaderSection(); | ||
pageHeaderSection.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D); | ||
pageHeaderSection.Name = "pageHeaderSection"; | ||
|
||
var detailSection = new Telerik.Reporting.DetailSection(); | ||
detailSection.Height = Telerik.Reporting.Drawing.Unit.Cm(5D); | ||
detailSection.Name = "detail"; | ||
|
||
var pageFooterSection = new Telerik.Reporting.PageFooterSection(); | ||
pageFooterSection.Height = Telerik.Reporting.Drawing.Unit.Cm(2.5D); | ||
pageFooterSection.Name = "pageFooterSection"; | ||
|
||
report.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { pageHeaderSection, detailSection, pageFooterSection }); | ||
```` | ||
|
||
### 3. Preparing the Data Source | ||
|
||
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%}): | ||
|
||
````csharp | ||
string selectCommand = "SELECT * FROM production.productphoto;"; | ||
string connectionString = "server=localhost\\sqlexpress;database=AdventureWorks2022;trusted_connection=true;"; | ||
var sqlDataSource = new Telerik.Reporting.SqlDataSource(connectionString, selectCommand); | ||
sqlDataSource.Name = "sqlDataSource"; | ||
sqlDataSource.ProviderName = "System.Data.SqlClient"; | ||
```` | ||
|
||
## Solution | ||
|
||
Once the report structure and data source are ready, you can proceed with creating the table. | ||
|
||
### 1. Create the Table | ||
|
||
Instantiate a `Telerik.Reporting.Table` object, assign the pre-created data source, and add the table to the detail section: | ||
|
||
````csharp | ||
var table = new Telerik.Reporting.Table(); | ||
table.Name = "table"; | ||
table.Location = new Telerik.Reporting.Drawing.PointU(Telerik.Reporting.Drawing.Unit.Cm(1.5D), Telerik.Reporting.Drawing.Unit.Cm(1.5D)); | ||
table.DataSource = sqlDataSource; | ||
detailSection.Items.Add(table); | ||
```` | ||
|
||
### 2. Add a Detail Row Group | ||
|
||
Add a detail row group and a body row. | ||
|
||
````csharp | ||
var rowGroup = new Telerik.Reporting.TableGroup(); | ||
rowGroup.Name = "detailTableGroup"; | ||
rowGroup.Groupings.Add(new Telerik.Reporting.Grouping(null)); | ||
table.RowGroups.Add(rowGroup); | ||
table.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Cm(0.609D))); | ||
```` | ||
|
||
>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. | ||
|
||
### 3. Add Columns Dynamically Based on Data | ||
|
||
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. | ||
|
||
> 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. | ||
|
||
````csharp | ||
using (var connection = new System.Data.SqlClient.SqlConnection(connectionString)) | ||
{ | ||
// retrieve the column names from the data source | ||
var command = new System.Data.SqlClient.SqlCommand(selectCommand, connection); | ||
connection.Open(); | ||
var reader = command.ExecuteReader(); | ||
var dataTable = new System.Data.DataTable(); | ||
dataTable.Load(reader); | ||
var columnNames = dataTable.Columns.Cast<System.Data.DataColumn>().Select(c => c.ColumnName); | ||
|
||
// for each column name | ||
int colIndex = 0; | ||
foreach (string columnName in columnNames) | ||
{ | ||
// add column | ||
var column = new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Cm(2.99D)); | ||
table.Body.Columns.Add(column); | ||
|
||
// add column header | ||
var headerTextBox = new Telerik.Reporting.TextBox(); | ||
headerTextBox.Name = columnName + "TextBoxHeader"; | ||
headerTextBox.Value = columnName; | ||
headerTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2.99D), Telerik.Reporting.Drawing.Unit.Cm(0.609D)); | ||
|
||
var columnGroup = new Telerik.Reporting.TableGroup(); | ||
columnGroup.Name = columnName; | ||
columnGroup.ReportItem = headerTextBox; | ||
table.ColumnGroups.Add(columnGroup); | ||
|
||
// add column body | ||
var bodyTextBox = new Telerik.Reporting.TextBox(); | ||
bodyTextBox.Name = columnName + "TextBoxBody"; | ||
bodyTextBox.Value = "= Fields." + columnName; | ||
bodyTextBox.Size = new Telerik.Reporting.Drawing.SizeU(Telerik.Reporting.Drawing.Unit.Cm(2.99D), Telerik.Reporting.Drawing.Unit.Cm(0.609D)); | ||
table.Body.SetCellContent(0, colIndex, bodyTextBox); | ||
|
||
colIndex++; | ||
} | ||
} | ||
```` | ||
|
||
## Demo | ||
|
||
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). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.