C# 使用RDLC中的嵌套列表对象绑定对象数据源

C# 使用RDLC中的嵌套列表对象绑定对象数据源,c#,rdlc,C#,Rdlc,我知道我们可以将对象数据源绑定到RDLC。我正在尝试绑定一个对象,如下所示: public class ContactReportData { public string ContactReportHeading{get;set;} public string ContactReportSubHeading{get;set;} public List<Contact> ContactDetails{get;set;} } public class Contact

我知道我们可以将对象数据源绑定到RDLC。我正在尝试绑定一个对象,如下所示:

public class ContactReportData
{
   public string ContactReportHeading{get;set;}
   public string ContactReportSubHeading{get;set;}
   public List<Contact> ContactDetails{get;set;}
}

public class Contact    {
   public string ContactName{get;set;}
   public string ContactDesignation{get;set;}
}
公共类ContactReportData
{
公共字符串ContactReportHeading{get;set;}
公共字符串ContactReportSubHeading{get;set;}
公共列表联系人详细信息{get;set;}
}
公共类联系人{
公共字符串ContactName{get;set;}
公共字符串名称{get;set;}
}
当设置为RDLC时,它一次只能找到一个数据集,比如ContactReportData(没有联系人列表)或contact。我想这是因为报告需要这样做

我希望报告中的数据显示为: 联系人报告标题 副标题 表格式的触点-触点名称、名称


我本可以将此作为报告数据的详细信息,作为静态数据,只绑定联系人列表,但我拥有的是我面临问题的联系人报告列表。

我通过将我的对象作为匿名对象的平面列表重新运行来解决此问题,该匿名对象具有联系人的所有属性以及父对象的其他属性。然后在RDLC报告中,添加一个表并绑定contact对象的属性,然后向父属性添加一个组


总结是为了制作这样的布局,你需要添加分组。

这可能不是一个合适的答案,但是当我觉得缺少关于这个主题的材料时,鼓励我发布我的发现

假设我在父对象中有一个嵌套的子对象列表。这是一种非常常见的情况,例如,如果您有一个order对象(父对象),您可能会有一个order项(子项)列表,您如何使用rdlc显示所有信息?有两种方法,一种是使用子报表,另一种是使用分组。我意识到它们都可以实现相同的功能,即在报告上显示详细信息列表

public class Order{
    public int OrderID {get; set;}
    public string Descrpition {get; set;}
    public List<OrderItem> OrderItems {get; set;}
}
public class OrderItem{
    public int OrderItemID {get; set;}
    public decimal Price{get; set;}
}

然后在rdlc上,您只需创建父行组和子行组,父行组应按OrderID分组,子行组应设置为“显示详细信息”。我认为您可以多次这样做,以实现对象的多级嵌套列表。

您真的不必展平对象。相反,您可以将多个数据集绑定到报表。然后,您可以通过代码将多个报表数据源分配给报表。以下是工作示例:

List<Loan> loans = new List<Loan>();
loans.Add(GetLoanByLoanNumber(loanNumber));

LocalReport report = new LocalReport();
report.ReportPath = HostingEnvironment.MapPath("~/bin/Report/Receipt.rdlc");

ReportDataSource loanDetailsDataSource = new ReportDataSource();
loanDetailsDataSource.Name = "LoanDataSet"; //This refers to the dataset name in the RDLC file
loanDetailsDataSource.Value = loans;
report.DataSources.Add(loanDetailsDataSource);

ReportDataSource loanItemsDataSource = new ReportDataSource();
loanItemsDataSource.Name = "LoanItemsDataSet";
loanItemsDataSource.Value = loans[0].loanItems;
report.DataSources.Add(loanItemsDataSource);

ReportDataSource principalPaymentDataSource = new ReportDataSource();
principalPaymentDataSource.Name = "PrincipalPaymentDataSet";
principalPaymentDataSource.Value = loans[0].principalPayments;
report.DataSources.Add(principalPaymentDataSource);

ReportDataSource interestPaymentDataSource = new ReportDataSource();
interestPaymentDataSource.Name = "InterestPaymentDataSet";
interestPaymentDataSource.Value = loans[0].interestPayments;
report.DataSources.Add(interestPaymentDataSource);
List loans=new List();
添加(GetLoanBylanumber(loanNumber));
LocalReport=新的LocalReport();
report.ReportPath=HostingEnvironment.MapPath(“~/bin/report/receive.rdlc”);
ReportDataSource loanDetailsDataSource=新的ReportDataSource();
loanDetailsDataSource.Name=“LoanDataSet”//这是指RDLC文件中的数据集名称
LoandDetailsDataSource.Value=贷款;
report.DataSources.Add(loanDetailsDataSource);
ReportDataSource loanItemsDataSource=新的ReportDataSource();
loanItemsDataSource.Name=“LoanItemsDataSet”;
loanItemsDataSource.Value=贷款[0]。loanItems;
report.DataSources.Add(loanItemsDataSource);
ReportDataSource principalPaymentDataSource=新的ReportDataSource();
principalPaymentDataSource.Name=“PrincipalPaymentDataSet”;
principalPaymentDataSource.Value=贷款[0]。principalPayments;
report.DataSources.Add(principalPaymentDataSource);
ReportDataSource interestPaymentDataSource=新的ReportDataSource();
interestPaymentDataSource.Name=“InterestPaymentDataSet”;
interestPaymentDataSource.Value=贷款[0]。interestPayments;
report.DataSources.Add(interestPaymentDataSource);

希望这能帮助别人

@Bharath说得很对,您可以将一个报表分配给多个数据源,答案中的代码包括将数据分配给报表

如果您最初是通过设计器创建报表,那么报表的两个数据源如下所示:

要获取第二个数据集,请右键单击树中的“数据集”文件夹,然后选择“添加数据集…”


然后,您可以使用报告中两个数据集中的字段;在上面的屏幕截图中,文本框链接到数据集1,表格由数据集2填充。

对象列表绑定为常规报告字段,但类(定义此对象)应标记为
[Serializable]
属性。

在报告中,字段用作
=First(Fields!NameOfListProperty.Value.Count,“DataSet1”)
字段!NameOfListProperty.Value(0).Item
作为数据列表的常规属性进行示例。

这里也有同样的问题。我开始在主表中添加一个列表,但不知道如何绑定它。OT:我希望您已经安装了VS2010 SP1。这是我在搜索答案时首先遇到的问题。您能进一步解释一下如何在RDLC上迭代列表吗?我已经试过了,但它没有呈现出值。添加分组是什么意思?如果贷款列表有多个项目怎么办?@BlueM loans不必是列表。它也可以是带有一组属性的类,每个属性都可以是一个列表。如果Dataset1是一个父数据列表,Dataset2代表一对多的父到子列表,那么这个方法有效吗?@PaulGibson我不知道,我还没有试过,我担心这个方法有效吗?我仍然无法指定第三个数据集。
List<Loan> loans = new List<Loan>();
loans.Add(GetLoanByLoanNumber(loanNumber));

LocalReport report = new LocalReport();
report.ReportPath = HostingEnvironment.MapPath("~/bin/Report/Receipt.rdlc");

ReportDataSource loanDetailsDataSource = new ReportDataSource();
loanDetailsDataSource.Name = "LoanDataSet"; //This refers to the dataset name in the RDLC file
loanDetailsDataSource.Value = loans;
report.DataSources.Add(loanDetailsDataSource);

ReportDataSource loanItemsDataSource = new ReportDataSource();
loanItemsDataSource.Name = "LoanItemsDataSet";
loanItemsDataSource.Value = loans[0].loanItems;
report.DataSources.Add(loanItemsDataSource);

ReportDataSource principalPaymentDataSource = new ReportDataSource();
principalPaymentDataSource.Name = "PrincipalPaymentDataSet";
principalPaymentDataSource.Value = loans[0].principalPayments;
report.DataSources.Add(principalPaymentDataSource);

ReportDataSource interestPaymentDataSource = new ReportDataSource();
interestPaymentDataSource.Name = "InterestPaymentDataSet";
interestPaymentDataSource.Value = loans[0].interestPayments;
report.DataSources.Add(interestPaymentDataSource);