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