开发者

Binding an rdlc report with a business object

开发者 https://www.devze.com 2023-01-21 22:28 出处:网络
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.

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 a开发者_StackOverflownd Email.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消