7

I know we can bind object data source to RDLC. I am trying to bind an object like as follows:

public class ContactReportData
{
   public string ContactReportHeading{get;set;}
   public string ContactReportSubHeading{get;set;}
   public List<Contact> ContactDetails{get;set;}
}

public class Contact    {
   public string ContactName{get;set;}
   public string ContactDesignation{get;set;}
}

While Setting to RDLC, it will find only one dataset at time, say ContactReportData (without the list of contact) OR Contact. I guess that is because the report needs it to be in that way.

What I want is to have the data shown in report as : ContactReportHeading ContactReportSubHeading tabular form of Contacts in - Contact Name, Designation.

I could have given this as the details of report data as static and only bind the contacts list, but what I have is list of contact reports where I a facing the issue.

Chinjoo
  • 2,697
  • 6
  • 28
  • 45
  • Same problem here. I started with adding a list in my main table but no idea how I bind it. OT: I hope you have VS2010 SP1 installed. That's what I hit first while searching for an answer. – Ε Г И І И О Jan 19 '12 at 07:16

5 Answers5

4

You really don't have to flatten your objects. Instead you can bind multiple datasets to report. Then you can assign multiple report data source to your report via code. Here is the working sample:

List<Loan> loans = new List<Loan>();
loans.Add(GetLoanByLoanNumber(loanNumber));

LocalReport report = new LocalReport();
report.ReportPath = HostingEnvironment.MapPath("~/bin/Report/Receipt.rdlc");

ReportDataSource loanDetailsDataSource = new ReportDataSource();
loanDetailsDataSource.Name = "LoanDataSet"; //This refers to the dataset name in the RDLC file
loanDetailsDataSource.Value = loans;
report.DataSources.Add(loanDetailsDataSource);

ReportDataSource loanItemsDataSource = new ReportDataSource();
loanItemsDataSource.Name = "LoanItemsDataSet";
loanItemsDataSource.Value = loans[0].loanItems;
report.DataSources.Add(loanItemsDataSource);

ReportDataSource principalPaymentDataSource = new ReportDataSource();
principalPaymentDataSource.Name = "PrincipalPaymentDataSet";
principalPaymentDataSource.Value = loans[0].principalPayments;
report.DataSources.Add(principalPaymentDataSource);

ReportDataSource interestPaymentDataSource = new ReportDataSource();
interestPaymentDataSource.Name = "InterestPaymentDataSet";
interestPaymentDataSource.Value = loans[0].interestPayments;
report.DataSources.Add(interestPaymentDataSource);
Bharath
  • 195
  • 1
  • 1
  • 19
4

I solved this by making my object retun as a flat list of anonymous object having all the properties of contact as well as the additional properties of parent object. Then in the RDLC report, added a table and bound the properties of contact object and then added a groups to the parent properties.

Summary is for making such layout you need to add grouping.

tomash
  • 12,742
  • 15
  • 64
  • 81
Chinjoo
  • 2,697
  • 6
  • 28
  • 45
  • 1
    could you explain further how do you iterate over the list on the RDLC? I have tried that and it doesn't render the value. What do you mean by adding grouping? – diegosasw Jun 09 '15 at 04:15
2

This is probably not an appropriate answer, but when I feel like the lack of material on this subject encourage me to post about my findings.

Let's say If I have a nested list of children object within the parent object. This is a very common situation for example, if you have an order object(parent), you will probably have a list of order items(children), how do you display all the information with the rdlc? There are two ways, 1 using subreport, and 2 is to use grouping. I realize they can both achieve the same thing which is displaying list of details on a report.

public class Order{
    public int OrderID {get; set;}
    public string Descrpition {get; set;}
    public List<OrderItem> OrderItems {get; set;}
}
public class OrderItem{
    public int OrderItemID {get; set;}
    public decimal Price{get; set;}
}

The easiest way is to use grouping. With grouping, you have to create a new datatype that contains the properties of the parent and children. I believe this way works with multi-level nested list of objects also. It might sound stupid, but most of the time you have to create a new datatype anyway because the types you need to display on the report are different from the business objects:

public class OrderReport{
    public int OrderID {get; set;}
    public string Description {get; set;}
    public int OrderItemID {get; set;}
    public decimal Price {get; set;}
}

Then on the rdlc, you just have to create parent row group and a child row group, Parent should be grouped by OrderID, the child row group should be set to "show details". I think you can do this multiple times to achieve multi-level nested list of objects.

gavin
  • 1,276
  • 1
  • 11
  • 17
1

List of Objects is bound as regular report field but Class (that defines this object) should be marked with [Serializable] attribute.
In the report, field is used as =First(Fields!NameOfListProperty.Value.Count, "DataSet1") or Fields!NameOfListProperty.Value(0).Item for examples as regular properties for a list of data.

Oleg Bondarenko
  • 1,694
  • 1
  • 16
  • 19
  • 1
    This should be marked as the right answer. It is the only one that has worked for me!!! – Carlos Sep 08 '22 at 08:34
  • 1
    @Carlos it is just the solution for actual RDLC report version . Previous answers are correct too, but looks to years when it were answered: these solutions are for old report versions. That is reason why other solutions are a bit awkward. – Oleg Bondarenko Sep 08 '22 at 08:57
0

@Bharath is quite right in saying you can assign a report more than one DataSource, and the code in that answer covers assigning the data to the report.

If you're creating the report through the designer intially, then having two data sources for the report looks like this:

RDLC Report Designer with two DataSets

To get the second dataset, right click on the Datasets folder in the tree and choose to 'Add Dataset..'

You can then use fields from both Datasets on the report; in the screenshot above the TextBoxes are linked to DataSet1 and the table is populated from DataSet2.

tomRedox
  • 28,092
  • 24
  • 117
  • 154