About Dynamic Data to Table

1 Answer 39 Views
.NET Framework Report Viewer - ASP.NET
Lin
Top achievements
Rank 1
Lin asked on 14 Dec 2024, 11:56 AM
I'm developing an ASP.NET report

My table header is obtained from the incoming data and is not fixed.
So I want to dynamically add Columns and put the content I specify, such as where the picture is circled

But no matter how I adjust it, I can't read my fields.

Even if I use static data for testing, it always tells me
"An error has occurred while processing Table 'table1':
Table Body has 3 rows but 1 are expected."

How should I write report code to achieve the results I want?

Attached is my code Hope you can help me answer it
Thank you very much

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 18 Dec 2024, 12:58 PM

Hello Lin,

Thank you for the attached report!

The cause for the issue is that a body row is added to the table for each row in the DataTable object:

            for (int rowIndex = 0; rowIndex < dataTable.Rows.Count; rowIndex++)
            {
                table.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Inch(0.2))); // 行高度 0.2 英寸
            }

When a row is created, it is also necessary to create a new table group, and if the row is meant to be part of an existing row group, then this group should be added to its child groups - TableGroup.ChildGroups, or to the Table.RowGroups if it is a parent row group.

In this case, there are no other row groups, so this should be a parent row detail group. A detail group is created by passing null to the Telerik.Reporting.Grouping constructor, for example:

            table.Body.Rows.Add(new Telerik.Reporting.TableBodyRow(Telerik.Reporting.Drawing.Unit.Cm(4)));

            var tableGroup = new Telerik.Reporting.TableGroup();
            tableGroup.Groupings.Add(new Telerik.Reporting.Grouping(null));
            tableGroup.Name = "detailTableGroup";
            table.RowGroups.Add(tableGroup);

Additionally, when you create the textbox items for the table column and row cells, pelase make sure to set both the Width and the Height, otherwise you may encounter an "Object reference not set to an instance of object" errors:

            foreach (System.Data.DataColumn test in dataTable.Columns)
            {
                table.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Telerik.Reporting.Drawing.Unit.Cm(4))); // 每列宽度 1 英寸

                // 添加列头
                var columnGroup = new Telerik.Reporting.TableGroup();
                var headerTextBox = new Telerik.Reporting.TextBox
                {
                    Value = test.ColumnName,
                    Name = test.ColumnName,
                    Height = Telerik.Reporting.Drawing.Unit.Cm(2),
                    Width = Telerik.Reporting.Drawing.Unit.Cm(4),
                    Style = { Font = { Bold = true }, TextAlign = Telerik.Reporting.Drawing.HorizontalAlign.Center, BackgroundColor = Color.LimeGreen }
                };

                columnGroup.ReportItem = headerTextBox;
                columnGroup.Name = test.ColumnName;

                table.ColumnGroups.Add(columnGroup);
                table.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { headerTextBox });
            }

Lastly, the DataTable object should be set to the DataSource property of the table:

            table.DataSource = dataTable;

I have attached the edited report to demonstrate those changes.

With that being said, we do not recommend designing reports via code as there are lots of situation where a small miss-step such as not setting a width or height of an item, could cause strange errors.

We recommend designing reports using our report designers and in code, only small adjustments should be made such as changing the data source of an item dynamically, the report page settings, etc.

I hope that the provided information and example will help.

Regards,
Dimitar
Progress Telerik

Stay tuned by visiting our roadmap and feedback portal pages, enjoy a smooth take-off with our Getting Started resources, or visit the free self-paced technical training at https://learn.telerik.com/.
Tags
.NET Framework Report Viewer - ASP.NET
Asked by
Lin
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or