将对象数据源与RDLC中的嵌套列表对象绑定

问题描述 投票:5回答:5

我知道我们可以将对象数据源绑定到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;}
}

设置为RDLC时,它只会找到一个数据集,比如ContactReportData(没有联系人列表)或联系人。我想这是因为报告需要它以这种方式。

我想要的是将报告中显示的数据显示为:ContactReportHeading ContactReportSubHeading表格形式的联系人 - 联系人姓名,名称。

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

c# rdlc
5个回答
4
投票

我通过使我的对象重新作为具有所有联系属性的匿名对象的平面列表以及父对象的附加属性来解决这个问题。然后在RDLC报告中添加一个表并绑定联系对象的属性,然后将一个组添加到父属性。

总结是为了进行这样的布局,您需要添加分组。


3
投票

你真的不需要压扁你的物体。相反,您可以绑定多个数据集以进行报告。然后,您可以通过代码为报表分配多个报表数据源。这是工作样本:

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

希望这会对某人有所帮助!


0
投票

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

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

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

最简单的方法是使用分组。使用分组,您必须创建一个包含父项和子项属性的新数据类型。我相信这种方式也适用于多级嵌套的对象列表。这可能听起来很愚蠢,但大多数时候你必须创建一个新的数据类型,因为你需要在报表上显示的类型与业务对象不同:

public class OrderReport{
    public int OrderID {get; set;}
    public string Description {get; set;}
    public int OrderItemID {get; set;}
    public decimal Price {get; set;}
}

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


0
投票

@Bharath说你可以为一个以上的数据源分配报告是完全正确的,该答案中的代码包括将数据分配给报告。

如果您是通过设计人员创建报表,那么为报表提供两个数据源如下所示:

RDLC Report Designer with two DataSets

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

然后,您可以在报告中使用两个数据集中的字段;在上面的屏幕截图中,TextBoxes链接到DataSet1,表格从DataSet2填充。


0
投票

对象列表绑定为常规报告字段,但Class(定义此对象)应使用[Serializable]属性标记。 在报告中,字段用作=First(Fields!NameOfListProperty.Value.Count, "DataSet1")Fields!NameOfListProperty.Value(0).Item,例如作为数据列表的常规属性。

© www.soinside.com 2019 - 2024. All rights reserved.