我正在尝试让嵌套对象在 Microsoft 报告中工作。我从http://www.gotreportviewer.com/objectdatasources/index.html下载了示例代码,它运行正常。
我基于 Windows 窗体及其代码构建了以下小应用程序,当我引用嵌套对象值时,我得到的只是数据应该出现的地方的“#Error”。
在报告中,我使用了网站推荐的相同嵌套对象语法:
=Fields!Name.Value.FirstName
它适用于他们在我的计算机上的应用程序,但不适用于我的。我无法理解!有没有人遇到过这种情况,或者知道为什么会这样?
此外 - 我不知道这是否相关 - 我无法将 ClientItem 的单个实例添加到 LocalReport.DataSources 对象。它必须是一个列表。但是,当它呈现时,它只在报表的表格中显示一行 (#Errored) 数据。
任何帮助,将不胜感激!
namespace ReportTest
{
public class ClientItem
{
public int Id { get; set; }
public ClientName Name { get; set; }
}
public class ClientName
{
public ClientName(string first, string last)
{
FirstName = first;
LastName = last;
}
string FirstName { get; set; }
string LastName { get; set; }
}
public partial class Form1 : Form
{
private List<ClientItem> clients = new List<ClientItem>();
public Form1()
{
InitializeComponent();
PopulateLists();
GenerateReport();
}
private void PopulateLists()
{
clients.Add(new ClientItem { Id = 1, Name = new ClientName("Adrian", "Adesco") } );
clients.Add(new ClientItem { Id = 2, Name = new ClientName("Brian", "Briar") } );
clients.Add(new ClientItem { Id = 3, Name = new ClientName("Clive", "Cussler") } );
}
private void GenerateReport()
{
this.Text = "Report Control Demo";
this.ClientSize = new System.Drawing.Size(950, 600);
ReportViewer reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.ReportPath = "TestReport.rdlc";
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportTest_ClientItem", clients));
reportViewer.Dock = DockStyle.Fill;
this.Controls.Add(reportViewer);
reportViewer.RefreshReport();
}
}
}