以集合作为数据源的 Microsoft 报告

发布于 2024-08-04 10:05:22 字数 1130 浏览 2 评论 0原文

我正在使用 MS Reporting Services。底层数据源是

IEnumerable,我没有使用数据集。

每个MyObject 都有属性和一些其他IEnumerable 集合。 在报告中,我想显示 MyObject 中的所有属性和 收藏品列表也有。 我不知道如何显示这个内部集合,所以我制作了一个 SubReport ,向其中传递了 MyObject.Id ,以便 SubReport 可以通过以下方式检索对象:他自己并为这些内部集合构建一个数据源。 我在这次活动中这样做。

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

但我的主报告中总是出现“子报告无法显示”。 (主报告 - 子报告已正确绑定)

知道为什么吗?或者如何以更优雅的方式解决这个问题?

谢谢

I'm working with MS Reporting Services. The underlying datasource is

IEnumerable<MyObject>, I'm not using DataSets.

Every MyObject has properties and some other IEnumerable collections.
In the report I want to display all the properties from MyObject and
the collections lists too.
I didn't know how to display this inner collections, so I've made a SubReport to which I passed the MyObject.Id so that the SubReport could retrieve the object by himself and Build a the DataSource for these inner collections.
I do this in this event.

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

But there is always "The SubReport could not be shown" in my Master Report.
(Master report - subreport are correctly binded)

Any Idea why? Or how to resolve this in a more elegant way ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2024-08-11 10:05:22

好的。

所以我去了这个解决方案并且它正在工作:

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

OK.

So I went to this solution and it's working:

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));              
    }           
}
耳钉梦 2024-08-11 10:05:22

如果我理解正确的话,你有一个类似于表格的结构。那你为什么不拿一个DataTable呢? ReportingServices 可以轻松访问这些内容。
还是我误会你了?

If I understand you correctly, you have a structure that resembles a table. So why don't you take a DataTable? ReportingServices offers easy access to those.
Or did I get you wrong there?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文