【问题标题】:Binding an rdlc report with a business object将 rdlc 报告与业务对象绑定
【发布时间】:2010-10-26 22:30:27
【问题描述】:

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

在我的报告中,我有名为名称和电子邮件的文本框。

假设我有一个对象 Person,其属性为 Name 和 Email。

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

【问题讨论】:

    标签: c# .net asp.net reporting rdlc


    【解决方案1】:

    是的,只需创建一个新的 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);
    

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

    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 };
    

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

    【讨论】:

    • 那么,我将如何在报表上绑定非列表元素。例如,假设我有一个具有以下属性的对象 Event:Name、Location、List Persons。我想将报表中的文本框与事件名称和位置绑定,并将报表中的列表与人员绑定。我怎么能这样做?
    • 我使用非重复项的参数来做到这一点。这是执行此操作的标准方法吗?
    • 我避免使用报告参数,只是因为我不喜欢弄乱它们,但如果它对你有用,那就太好了。我编辑了我的答案,以展示另一种无需使用 Linq 参数即可完成的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2017-11-04
    • 2019-01-08
    相关资源
    最近更新 更多