Reporting Services:具有父子孙的业务对象数据源

发布于 2025-01-05 04:47:26 字数 549 浏览 1 评论 0原文

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

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 文件)。

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 }));
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2025-01-12 04:47:26

Invoice.rdlc

  • 添加一个名为 InvoiceDataset 的数据集(从“报表数据”工具窗口)
  • 添加一个列表控件(因为报表必须绑定到发票列表,即使该列表只包含一个元素)
  • 在列表控件中添加发票级别字段,例如客户名称
  • 在列表控件中添加一个指向 Account.rdlc 的子报表控件,其名称为“Account”,参数为 InvoiceId

Account.rdlc

  • 添加名为 AccountDataset 的数据集(从“报表数据”工具窗口)
  • 添加 InvoiceId 参数以匹配 Invoice.rdlc 中指定的参数 子报表控件
  • 添加列表控件
  • 在列表控件中添加帐户级别 帐号等字段
  • 列表控件中的

添加一个指向 LineItem.rdlc 的子报表,其中包含两个参数:InvoiceId 和 AccountId LineItem.rdlc

  • 添加名为 LineItemDataset 的数据集(来自报表数据工具)窗口)
  • 添加 InvoiceId 和 AccountId 参数以匹配 Account.rdlc 中指定的参数 子报表控件
  • 添加列表控件
  • 在列表控件中添加行项目级字段,如描述、数量、价格

要将此报表生成为 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");
}

Invoice.rdlc

  • Add a dataset named InvoiceDataset (from the Report Data tool window)
  • Add a List control (since the report must be bound to a list of Invoices even if the list will only ever contain one element)
  • Within the List control add Invoice-level fields like Customer Name
  • Within the List control add a Subreport control pointing to Account.rdlc with a name of "Account" and a parameter of InvoiceId

Account.rdlc

  • Add a dataset named AccountDataset (from the Report Data tool window)
  • Add an InvoiceId parameter to match the parameter specified in Invoice.rdlc Subreport control
  • Add a List control
  • Within the List control add Account-level fields like Account Number
  • Within the List control add a subreport pointing to LineItem.rdlc with two parameters: InvoiceId and AccountId

LineItem.rdlc

  • Add a dataset named LineItemDataset (from the Report Data tool window)
  • Add InvoiceId and AccountId parameters to match the parameters specified in Account.rdlc Subreport control
  • Add a List control
  • Within the List control add Line Item-level fields like Description, Quantity, Price

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文