我正在使用MS Reporting 。底层数据源是
IEnumerable<MyObject>,我不使用DataSets。
每个MyObject都有属性和一些其他IEnumerable集合。在报告中,我还想显示MyObject和集合列表中的所有属性。我不知道如何显示这个内部集合,所以我做了一个 SubReport ,将MyObject.Id传递给它,这样SubReport就可以自己检索对象并为这些内部集合构建DataSource。我是在这件事上这么做的。
myReportViewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
int id;
if (e.Parameters.Count > 0 && int.TryParse(e.Parameters[0].Values[0], out id))
{
MyObject current = myObjects.Where(x => x.MyObject.Id == id).FirstOrDefault();
InnerListBindingSource.DataSource = current.InnerCollection;
e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource(
"MyInnerCollectionDataSource", InnerListBindingSource));
}
}但总有“SubReport不能显示”在我的硕士报告。(主报表-子报表正确绑定)
知道为什么吗?或者如何以一种更优雅的方式解决这个问题?
谢谢
发布于 2009-09-10 05:57:44
好的。
所以我找到了这个解决方案,它起作用了:
private IEnumerable<MyObject> myObjects;
public ReportViewerForm(IEnumerable<MyObject> myObjects)
{
InitializeComponent();
this.myObjects = myObjects;
this.WindowState = FormWindowState.Maximized;
ReportViewer reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.LocalReport.ReportEmbeddedResource = @"SomePath." + "Report1.rdlc";
/*reportViewer.LocalReport.ReportPath = @"SomePath\Report1.rdlc"; */
reportViewer.LocalReport.SubreportProcessing +=
new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
reportViewer.LocalReport.DataSources.Add(
new ReportDataSource("MyDataSource", myObjects));
reportViewer.LocalReport.SetParameters(new List<ReportParameter>
{
new ReportParameter("param1", ..WhatEver..),
...
});
reportViewer.Dock = DockStyle.Fill;
this.panel1.Controls.Add(reportViewer);
reportViewer.RefreshReport();
}
void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
/* For example ID parsing.. when you have it defined in .rdlc file.. */
int id;
if (e.Parameters.Count > 0 && int.TryParse(e.Parameters[0].Values[0], out id))
{
MyObject current = myObjects.Where(x => x.MyObject.Id == id).FirstOrDefault();
e.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource(
"InnerListDataSource", current.InnerList));
}
}发布于 2009-09-09 08:18:08
如果我没听错的话,你的结构就像一张桌子。那你为什么不拿个DataTable呢?ReportingServices提供了对这些信息的轻松访问。还是我搞错你了?
https://stackoverflow.com/questions/1398247
复制相似问题