|
| 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 | +} |
0 commit comments