views:

21

answers:

1

Is it possible to bind a rdlc report to a business object (.NET 4/VS 2010)

In my report I have TextBoxes called Name and Email.

Say I have an object Person with properties Name and Email.

Can I bind the .rdlc with an object Person at runtime?

+1  A: 

Yes it is, just create a new ReportDataSource:

var people = new List<Person>();
var reportDataSource = new Microsoft.Reporting.WebForms.ReportDataSource {Name = "DataSet1", Value = people};

var report = new Microsoft.Reporting.WebForms.LocalReport();
report.DataSources.Add(reportDataSource);

If you object has collection properties you can flatten the data before you send it to the report, then use grouping to show the hierarchy:

var myEvent = new Event("Test Name", "Test Location", new List<Person>());
var reportData = myEvent.Persons.Select(p => new { myEvent.EventName, myEvent.EventLocation, p.Name, p.Email });
var reportDataSource = new Microsoft.Reporting.WebForms.ReportDataSource { Name = "DataSet1", Value = reportData };

There might be a better way to get at the object properties, but I haven't found it yet.

Sprockincat
So, how would I bind non-list elements on the report. For example, let's say I had an object Event with these properties: Name, Location, List<Person> Persons. I want to bind the Textboxes in the report with the Event Name and Location, and the List in the report with Persons. How could I do that?
Prabhu
I did it using parameters for non-repeating items. Is that the standard way to do this?
Prabhu
I avoid report parameters, just because I don't like messing with them, but if it's working well for you then great. I edited my answer to show another way you could do it without parameters using Linq.
Sprockincat