将rdlc报告与业务对象绑定

是否可以将rdlc报告绑定到业务对象(.NET 4 / VS 2010)

在我的报告中,我有一个名为Name和Email的TextBox。

假设我有一个对象具有属性名称和电子邮件的人。

我可以在运行时将.rdlc与对象Person绑定吗?

是的,只需创建一个新的ReportDataSource:

var people = new List(); var reportDataSource = new Microsoft.Reporting.WebForms.ReportDataSource {Name = "DataSet1", Value = people}; var report = new Microsoft.Reporting.WebForms.LocalReport(); report.DataSources.Add(reportDataSource); 

如果对象具有集合属性,则可以在将数据发送到报表之前展平数据,然后使用分组来显示层次结构:

 var myEvent = new Event("Test Name", "Test Location", new List()); 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 }; 

可能有更好的方法来获取对象属性,但我还没有找到它。