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).
Invoice.rdlc
Account.rdlc
LineItem.rdlc
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With