Prabhu
Prabhu

Reputation: 13355

Binding an rdlc report with a business object

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?

Upvotes: 1

Views: 9690

Answers (1)

Walter
Walter

Reputation: 1855

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.

Upvotes: 2

Related Questions