如何在 Telerik 报告中使用具有动态列数的表创建子报告

我有一份使用 Telerik 报告制作的报告。此报告在表格中显示一个月中所有给定工作日的信息(例如 2017 年 7 月的所有星期一)。表格的列是天数,行是与日期相关的数据。

某一天落在一个月内的次数各不相同,因此我需要能够根据请求的数据定义表需要具有的列数。我该怎么做?

这里模拟了它应该如何显示:

http://img.mukewang.com/61a33ea300017cd204260377.jpg

慕标5832272
浏览 280回答 1
1回答

qq_遁去的一_1

考虑到报告名称是MainReport和SubReport:在其中MainReport添加以下代码:public static ReportSource SetReportSourceForSubreport(object data){&nbsp; &nbsp; var report = new SubReport(data);&nbsp; &nbsp; var repSource = new InstanceReportSource&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ReportDocument = report&nbsp; &nbsp; };&nbsp; &nbsp; return repSource;}现在通过 Visual Studio 报表设计器向主报表添加一个子报表,并将报表源设置为= MyNameSpace.MainReport.SetReportSourceForSubreport(Fields.Data). 确保它Data在主报告的数据源中可用。您SubReport现在需要一个接受数据的构造函数。首先调用该InitializeComponent()方法,然后通过代码生成表,添加所需的列数和行数。public SubReport(object data){&nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; // Create and add table to report body&nbsp; &nbsp; var table = CreateTable(data);&nbsp; &nbsp; this.detail.Items.Add(table);}关于如何通过代码生成表,请阅读以下页面和相关文章以获得详细说明。https://docs.telerik.com/reporting/table-understanding-cells-rows-columns关于如何用代码生成表格的一个小例子:private Table CreateTable(Dictionary<string, IEnumerable<string>> data){&nbsp; &nbsp; ////&nbsp; &nbsp; // New table instance&nbsp; &nbsp; ////&nbsp; &nbsp; _requiredColumns = data.Count + 1;&nbsp; &nbsp; Table table = new Table()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Name = "tableDay",&nbsp; &nbsp; &nbsp; &nbsp; Docking = DockingStyle.Fill,&nbsp; &nbsp; &nbsp; &nbsp; Location = new PointU(Unit.Cm(0D), Unit.Cm(0D)),&nbsp; &nbsp; &nbsp; &nbsp; Size = new SizeU(Unit.Cm(17D), Unit.Cm(5D)),&nbsp; &nbsp; &nbsp; &nbsp; RowHeadersPrintOnEveryPage = true&nbsp; &nbsp; };&nbsp; &nbsp; table.Bindings.Add(new Telerik.Reporting.Binding("DataSource", "= Fields.Rows"));&nbsp; &nbsp; for (int i = 0; i < _requiredColumns; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; table.Body.Columns.Add(new TableBodyColumn(Unit.Cm(_columnWidth)));&nbsp; &nbsp; }&nbsp; &nbsp; ////&nbsp; &nbsp; // Add headers&nbsp; &nbsp; ////&nbsp; &nbsp; table.ColumnGroups.Add(new TableGroup&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Name = "columnLeftMost",&nbsp; &nbsp; &nbsp; &nbsp; ReportItem = new TextBox { Name = "textBoxHours", Value = "Hours" }&nbsp; &nbsp; });&nbsp; &nbsp; foreach (var item in data)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; table.ColumnGroups.Add(new TableGroup&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "column" + item.Key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReportItem = new TextBox { Name = "textBoxTitleDay" + item.Key, Value = item.Key }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; ////&nbsp; &nbsp; // Add data rows&nbsp; &nbsp; ////&nbsp; &nbsp; var tableGroup28 = new TableGroup() { Name = "tableGroup280" };&nbsp; &nbsp; tableGroup28.Groupings.Add(new Telerik.Reporting.Grouping(null));&nbsp; &nbsp; table.RowGroups.Add(tableGroup28);&nbsp; &nbsp; table.Body.Rows.Add(new TableBodyRow(Unit.Cm(ROWHEIGHT)));&nbsp; &nbsp; List<ReportItemBase> list = new List<ReportItemBase>();&nbsp; &nbsp; for (int i = 0; i < _requiredColumns; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var tb = new TextBox&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "textBox" + i,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = i == 0 ? "= Fields.DayTimeFriendly" : "= Fields.RowValues"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; list.Add(tb);&nbsp; &nbsp; &nbsp; &nbsp; table.Body.SetCellContent(0, i, tb);&nbsp; &nbsp; }&nbsp; &nbsp; table.Items.AddRange(list.ToArray());&nbsp; &nbsp; return table;}
打开App,查看更多内容
随时随地看视频慕课网APP