我正在使用 rdlc 报告来报告一些业务对象。我使用子报表来报告嵌套对象(如此处所述)。
当使用常规的子对象列表时,例如 IList(Of Books),我为书籍创建了一个数据源,然后将其用作子报表的数据源。
当嵌套对象是 IList(Of String) 或其他原始类型列表时,我不太了解如何使用此技术。
在这种情况下进行报告的最佳方式是什么?
我有一个像这样的对象结构
订单 -> 列表:OrderItems(产品) -> 列表:AdditionalProducts(产品)
我有一个带有子报表和子子报表的主报表。
我通过以下方式解决了这个问题
在子报表中,我将参数传递给子报表。此参数具有产品名称的值,称为“thisproduct”。
子报表和子子报表都调用子报表处理函数。所以我需要在这里为两者分配数据源。
我为子报表(order.items)分配数据源
如果子子报表 (InternalOrderItemAdditionalProducts) 正在调用此函数,我添加了一个“if”。在这里我检查参数,并获取产品名称。然后我可以遍历订单项,找到与正在查看的产品相关的订单项,然后将子报表数据源分配为与该产品相关的附加产品。
private void SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
// assign subreport datasource
e.DataSources.Add(new ReportDataSource("OrderItems", order.Items));
// if the subsubreport is calling this then
if (e.ReportPath != null)
if (e.ReportPath == "InternalOrderItemAdditionalProducts")
{
// find the orderitem relating to this product
foreach (var orderitem in order.Items)
{
if (e.Parameters["thisproduct"].Values[0] == orderitem.ProductType.Name)
{
// assign the subsubreport datasource to be the additional products of that order item
e.DataSources.Add(new ReportDataSource("AdditionalProducts",
orderitem.Product.AllDescendentAdditionalProducts));
}
}
}
}
我遇到了类似的问题。我有一个运行自定义对象列表的报告。CustomObject 列表是从对包含对其所有 CustomObjects 的许多引用的 ParentObjects 列表的查询中填充的。该关系已被删除,而是在 ParentObject (ParentObject.CustomObjectName) 上使用了一个字符串列。现在我的报告收到一个包含所有 CustomObject 名称的字符串 []。
我的解决方案是使用单个字符串属性和构造函数创建一个包装器对象,以用作我的数据源。我将它命名为我的报告所期望的 CustomObject。
class CustomObject
{
public string Name {get; set;}
public CustomObject(string name)
{
Name = name;
}
}
我使用 LINQ 加载列表,在 select 语句中调用包装器构造函数
var wrappedObjects = from parent in GetParentObjects()
select new CustomObject(parent.CustomObjectName);
从报告中,您可以像往常一样为 CustomObject 类添加数据源,并像往常一样访问对象“=Fields!Name.Value”。