创建具有多个表(一对多关系)的rdlc报告 [英] Creating an rdlc report with multiple tables (one-to-many relationship)
问题描述
因此,我是rdlc的新手(实际上实际上是在报告).我有一个表,该表与另一个表具有一对多的关系,并且我试图在rdlc报告中将它们表示为每个项目的多个表 .
So, I'm new to rdlc (and reporting in general actually). 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.
注意:这些表最初是使用Entity Framework代码优先的创建的.
这是两个表(和父表):
现在,通常如果我只有 [Quotation]
和一些 [QuotationItem]
,我只需添加 [Quotation]中的信息
,然后来自每个 [QuotationItem]
的信息将在表格(Tablix)内的一行中表示.
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).
问题是:每个 [QuotationItem]
也有很多个 [QuotationItemQuantity]
(目前是三个),并且也需要代表他们.
The problem is: Each [QuotationItem]
also has many of [QuotationItemQuantity]
(currently three), and they need to be represented too.
因此,我的报告应如下所示:
So, my report should look something like this:
但是我对如何显示每个项目(QuotationItem)的多个表(或列表和表)感到困惑.我尝试了嵌套表和列表中的表,但这似乎不被允许(出现明细成员只能包含静态内部成员"的错误.错误).
But I'm stuck on how to display multiple tables (or a list and tables) for each item (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.
注意:如上所述,每个 QuotationItem
当前有3个数量,但是将来可能会更改,因此,如果列可以是动态的,那会很好,但是,这不是一个此时的要求.
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.
有什么建议吗?
推荐答案
好吧,我希望(并且仍然希望)得到一种更优雅的解决方案,而不是这个丑陋的解决方案.(我不得不现在使用),但无论如何它都应该做.
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:
- 使用VS设计器创建一个虚拟DataSet,并向其中添加一个DataTable.
- 将两个表中所有必需的列添加到此DataTable中.
- 将数据集添加为报表的数据源.
-
为了传递实际数据,我将使用与设计的结构相同的结构填充数据表,然后使用类似以下内容的数据表将其传递给报表:
- Created a dummy DataSet using the VS designer, and added one DataTable to it.
- Added all the required columns from both tables into this DataTable.
- Added the DataSet as the data source for the report.
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:
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));
}
- 在RDLC报告中仅创建一个表(Tablix),并使用单元格边框通过删除表"之间不需要的单元格的边框来使其看起来像3个表:
- 将虚拟数据集中的字段添加到第一个表(数量信息)和表上方(报价项目信息)的相应单元格中.
- 对于第二个和第三个表",只需使用表达式即可根据第一个表中的值进行计算.
注意:问题和答案中的某些字段/变量名称均已更改,但思路保持不变.
Note: Some fields/variables names were changed in both the question and the answer, but the idea stays the same.
这篇关于创建具有多个表(一对多关系)的rdlc报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!