Reporting Services:带有父子孙的业务对象数据源 [英] Reporting Services: Business object data source with parent-child-grandchild

查看:101
本文介绍了Reporting Services:带有父子孙的业务对象数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用POCO/自定义业务对象创建具有父子孙关系的报告?

How do I create a report with a parent-child-grandchild relationship using POCOs / custom business objects?

public class Invoice
{
  public List<Account> Accounts { get; set; }
}

public class Account
{
  public List<LineItem> LineItems { get; set; }
}

public void GenerateReport()
{
    var localReport = new LocalReport();
    localReport.LoadReportDefinition(GetEmbeddedResource("Invoice.rdlc"));
    localReport.DataSources.Add(new ReportDataSource("InvoiceDataset", new List<Invoice> { invoices }));
}

最好对子报表使用表"和列表"控件.具有本地处理功能的Reporting Services v10(.rdlc文件).

Preferably using Table and List controls over Subreports. Reporting Services v10 with Local Processing (.rdlc files).

推荐答案

Invoice.rdlc

  • 添加一个名为InvoiceDataset的数据集(从报告数据"工具窗口中)
  • 添加列表控件(因为报表必须绑定到发票列表,即使列表仅包含一个元素也是如此)
  • 在列表"控件中添加客户名称"之类的发票级别字段
  • 在List控件内,添加一个指向Account.rdlc的子报表控件,该控件的名称为"Account",参数为InvoiceId.

Account.rdlc

  • 添加一个名为AccountDataset的数据集(从报告数据"工具窗口中)
  • 添加InvoiceId参数以匹配Invoice.rdlc子报表控件中指定的参数
  • 添加列表控件
  • 在列表"控件中,添加帐户级别"字段,例如帐号"
  • 在List控件内,添加一个指向LineItem.rdlc的子报表,该报表具有两个参数:InvoiceId和AccountId

LineItem.rdlc

  • 添加一个名为LineItemDataset的数据集(从报告数据"工具窗口中)
  • 添加InvoiceId和AccountId参数以匹配Account.rdlc子报表控件中指定的参数
  • 添加列表控件
  • 在列表"控件中,添加订单项"级别的字段,例如说明",数量",价格"

要以pdf格式生成此报告,请执行以下操作:

To generate this report as a pdf:

public byte[] GenerateInvoicePdf(Invoice invoice)
{
    var localReport = new LocalReport();

    localReport.LoadReportDefinition(GetEmbeddedResource("Invoice.rdlc"));
    localReport.LoadSubreportDefinition("Account", GetEmbeddedResource("Account.rdlc"));
    localReport.LoadSubreportDefinition("LineItem", GetEmbeddedResource("LineItem.rdlc"));
    var datasource = new List<Invoice> {invoice};
    localReport.DataSources.Add(new ReportDataSource("InvoiceDataset", datasource));
    localReport.SubreportProcessing +=
        (o, args) =>
            {
                if (args.ReportPath == "Account")
                {
                    var invoiceId = long.Parse(args.Parameters["InvoiceId"].Values[0]);
                    var invoice = datasource.First(x => x.InvoiceId == invoiceId);
                    args.DataSources.Add(new ReportDataSource("AccountDataset", invoice.Accounts));
                }
                else if (args.ReportPath == "LineItem")
                {
                    var invoiceId = long.Parse(args.Parameters["InvoiceId"].Values[0]);
                    var accountId = long.Parse(args.Parameters["AccountId"].Values[0]);
                    var invoice = datasource.First(x => x.InvoiceId == invoiceId);
                    var account = invoice.Accounts.First(x => x.AccountId == accountId);
                    args.DataSources.Add(new ReportDataSource("LineItemDataset", account.LineItems));
                }
            };
    return localReport.Render("pdf");
}

这篇关于Reporting Services:带有父子孙的业务对象数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆