How do I create a report with a parent-child-grandchild relationship using POCOs / custom business objects?
public class Invoice
{
public List A
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");
}