Binding an rdlc report with a business object

[亡魂溺海] 提交于 2019-12-21 06:51:56

问题


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:


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.



来源:https://stackoverflow.com/questions/4028599/binding-an-rdlc-report-with-a-business-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!