[英]Creating an rdlc report with multiple tables (one-to-many relationship)
So, I'm new to rdlc (and reporting in general actually). 所以,我是rdlc的新手(并且实际上报道了)。 I have a table that has a one-to-many relationship with another table, and I'm trying to represent them in an rdlc report as multiple tables for each item . 我有一个与另一个表有一对多关系的表,我试图在rdlc报告中将它们表示为每个项目的多个表。
Note: The tables are originally created using Entity Framework code-first . 注意:这些表最初是使用Entity Framework代码优先创建的 。
Here are the two tables (and the parent one) : 这是两个表(和父表) :
Now, normally if I only have the [Quotation]
and some [QuotationItem]
s, I'd just add the info from the [Quotation]
on the top of the report, and the info from each [QuotationItem]
would be represented in a row inside a table (Tablix). 现在,通常如果我只有[Quotation]
和一些[QuotationItem]
,我只需要在报告顶部的[Quotation]
添加信息,并且每个[QuotationItem]
将在表格内的一行(Tablix)。
The problem is : Each [QuotationItem]
also has many of [QuotationItemQuantity]
(currently three), and they need to be represented too. 问题是 :每个[QuotationItem]
也有许多 [QuotationItemQuantity]
(目前是三个),它们也需要表示。
So, my report should look something like this: 所以,我的报告应该是这样的:
But I'm stuck on how to display multiple tables (or a list and tables) for each item (QuotationItem). 但我仍然坚持如何为每个项目(QuotationItem)显示多个表(或列表和表)。 I tried nested tables and tables inside a list, but this doesn't seem to be allowed (I get a "Detail members can only contain static inner members" error). 我在列表中尝试了嵌套的表和表,但这似乎不被允许(我得到一个“详细信息成员只能包含静态内部成员”错误)。
I read about sub-reports and I think this might be the way to go, but I'm not sure how to use sub-reports in this case, or if this is actually the right approach. 我读到了关于子报告的内容,我认为这可能是要走的路,但我不确定如何在这种情况下使用子报告,或者这是否是正确的方法。
Note: As mentioned above, each QuotationItem
currently has 3 quantities, but that might be changed in the future, so would be great if the columns can be dynamic, however, this isn't a requirement at this point. 注意:如上所述,每个QuotationItem
当前有3个数量,但将来可能会更改,因此如果列可以是动态的,那么这将是很好的,但是,此时这不是必需的。
Any suggestions? 有什么建议?
Well, I was hoping (and still do) to get a more elegant solution instead of this ugly one (which I had to use for now) but it does what it's supposed to anyway. 好吧,我希望(现在仍然)能够获得一个更优雅的解决方案, 而不是这个丑陋的解决方案(我现在必须使用它),但无论如何它都应该做到了。
I'm posting it as it might help someone with a similar problem. 我发布它,因为它可能会帮助有类似问题的人。 Here's what I did: 这是我做的:
In order to pass the actual data, I would fill a DataTable with the same structure as the designed one and then pass it to the report using something like the following: 为了传递实际数据,我将使用与设计数据相同的结构填充DataTable ,然后使用以下内容将其传递给报表:
private void Form_Load(object sender, EventArgs e) { rptViewerMain.LocalReport.ReportEmbeddedResource = "MyProjectName.QuotationReport.rdlc"; rptViewerMain.LocalReport.EnableExternalImages = true; if (QuotationInfo !=null && QuotationItems != null) { SetupReport(); } this.rptViewerMain.RefreshReport(); } private void SetupReport() { var param1 = new ReportParameter("MyFirstParameter", SomeValue); var param2 = new ReportParameter("MySecondParameter", SomeOtherValue); // etc // Pass Parameters rptViewerMain.LocalReport.SetParameters(new[] { param1, param2, "etc" }); // Prepare the DataTable and add the values to it DataTable dt = new MyDummyDataset.MyDesignedDataTable().Clone(); foreach (var qItem in QuotationItems) { dt.Rows.Add(qItem.ItemNumber, qItem.ItemDescription, "and", "so", "many", "more", "values"); } // Pass the DataTable to the report as the data source replacing the dummy DataSet rptViewerMain.LocalReport.DataSources.Add(new ReportDataSource("MyDummyDataset", dt)); }
Note: Some fields/variables names were changed in both the question and the answer, but the idea stays the same. 注意:某些字段/变量名称在问题和答案中都已更改,但这个想法保持不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.