我最近一直在研究报表查看器,但我遇到了一个无法解决的问题...
我正在尝试绑定来自对象集合(标题)的数据,其中每个对象都有一个子对象(行)集合。如何才能做到这一点?以下是我目前拥有的代码片段(somedata 是标头对象的集合)。
带有 ReportViewer 控件的 Windows 窗体具有以下内容:
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.LoadReportDefinition(GetReport());
reportViewer1.LocalReport.DataSources.Clear();
var dataSourcesNames = GetDataSourceNames();
var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
reportViewer1.RefreshReport();
标头对象:
public class ReportHeader{
readonly string id;
readonly List<ReportRow> rows;
public ReportData(Header h) {
this.id = h.Id;
rows = new List<ReportRow>();
foreach(RowObject o in h.Rows){
rows.Add(new ReportRow(o));
}
}
public string Id { get { return id; } }
public List<ReportRow> Rows { get { return rows;} }
}
行对象:
public class ReportRow{
readonly decimal sum;
readonly string type;
readonly string code;
public ReportDataRow(RowObject r) {
sum = r.Sum;
type = r.Type;
code = r.Code;
}
public decimal Sum { get { return sum; } }
public string Type { get { return type; } }
public string Code { get { return code; } }
}
我创建了一个具有 ReportHeader 的所有属性的报告和一个应该包含所有 ReportRows 的列表,但它似乎不起作用。唯一的解决方案是制作两个单独的集合,ReportHeader 集合和 ReportRow 集合,然后分别绑定它们,如下所示:
var headerSource = new ReportDataSource(dataSourcesNames[0], somedata);
reportViewer1.LocalReport.DataSources.Add(headerSource);
var rowSource = new ReportDataSource(dataSourcesNames[1], somedata.Rows);
reportViewer1.LocalReport.DataSources.Add(rowSource);
使用此解决方案,我需要在集合之间建立某种关系..?所以请帮忙。(请注意,我为我的问题简化了很多对象)