Search code examples
c#reportviewer

Subtable in ReportViewer


I need to display in a report viewer (rdlc file) a list of customer (One customer per page), and orders related to that customer (On same page of customer info) and comments. How do I insert the tablix so that there is a page break for each customer?

This is how it should look:

And this is how i'm looking to add data on report:

List<Customer> customers = db.Customers;

LocalReport.DataSources.Add(new ReportDataSource("Customers", customers))

public class Customer
{
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public List<Order> Orders { get; set; }
    public List<Comment> Comments { get; set; }
}

public class Order
{
    public string Product { get; set; }
    public int Qty { get; set; }
    public decimal Price { get; set; }
}

public class Comment
{
    public string Description { get; set; }
}

EDIT: Maybe I did not express properly. I dont know how to display each customer's orders and comments on list items. How do I need to group? How do i set DataSet to Sub Table (For orders and comments)


Solution

  • I finally use subreport:

    1. One big report for Title (Name and phone), subreports positions and page break

    2. One subreport for Orders (CustomersOrders.rdlc): Adding a subreport parameter (CustomerId)

    3. One subreport for Comments (CustomersComments.rdlc): Adding a subreport parameter (CustomerId)

    4. A static property on report renderer window that contains a list of customers

    On report renderer window add an SubreportProcessing handler:

    LocalReport.SubreportProcessing += Customers_SubreportProcessing;
    
    private void Customers_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
    {
        ReportParameterInfo customerId = e.Parameters.FirstOrDefault(c => c.Name == "CustomerId");
        if (customerId== null)
            return;
    
        Customer customer = _customersList
            .FirstOrDefault(c => c.CustomerId == customerId.Values.FirstOrDefault());
    
        if (e.ReportPath == "CustomersOrders") // Name for subreport CustomersOrders.rdlc
        {
            e.DataSources.Add(new ReportDataSource("Orders", customer.Orders));
        }
        else if (e.ReportPath == "CustomersComments") // Name for subreport CustomersComments.rdlc
        {
            e.DataSources.Add(new ReportDataSource("Comments", customer.Comments));
        }
    }