简体   繁体   English

创建包含多个表的rdlc报告(一对多关系)

[英]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: 这是我做的:

Preparing the data: 准备数据:

  • Created a dummy DataSet using the VS designer, and added one DataTable to it. 使用VS设计器创建了一个虚拟DataSet,并向其添加了一个DataTable。
  • Added all the required columns from both tables into this DataTable. 将两个表中的所有必需列添加到此DataTable中。
  • Added the DataSet as the data source for the report. 添加了DataSet作为报告的数据源。
  • 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)); } 

Designing the report: 设计报告:

  • Created only one table (Tablix) in the RDLC report and used the cell borders to make it look like 3 tables by removing the borders of the unwanted cells between the "tables" : 在RDLC报告中仅创建了一个表(Tablix),并使用单元格边框使其看起来像3个表,方法是删除“表”之间不需要的单元格的边框 在此输入图像描述
  • Added the fields from the dummy DataSet to the corresponding cells in the first table (the quantities info) and above the table (the quotation item info). 将虚拟DataSet中的字段添加到第一个表中的相应单元格(数量信息)和表格上方(引用项目信息)。
  • For the second and third "tables", simply used expressions to do the calculations based on the values from the first table. 对于第二个和第三个“表”,只需使用表达式根据第一个表中的值进行计算。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM