如何在WPF项目中向ReportViewer添加rdlc文件

10

我通过我的主窗口的XAML设计器在WPF应用程序中添加了一个ReportViewer,现在我想将一个现有的rdlc文件添加到它上面。

我希望我的报表查看器在启动时显示一个空的rdlc文件(不包含参数),然后在从我的数据表格(绑定到一个observablecollection)选择一行后相应地更改其参数,并显示已填充的报表定义而不是空的报表定义。

我将制作一个带有选定行作为命令参数和相关事件的按钮,我只需要能够向报表传递数据。 我意识到这不是一个简单的问题,所以我会尝试简化:

  1. 如何将现有的rdlc文件添加到ReportViewer(MVVM,WPF)中?
  2. 我按下一个按钮 - 相关命令将我的observablecollection中项作为参数获取(我的数据表格中的一行) - 如何将此项的数据部分传递给报表的未填充部分(或者如果已填充,就覆盖)?

我希望我已经表达清楚了。 预先感谢您的回答!

2个回答

3

当您设置初始化方法并使用正确的报表路径和数据集名称时,应如下所示。

private void initializeReport()
        {
            this.mform_components = new System.ComponentModel.Container();
            Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();

            this.ProductBindingSource = new System.Windows.Forms.BindingSource(this.mform_components);
            ((System.ComponentModel.ISupportInitialize)(this.ProductBindingSource)).BeginInit();

            reportDataSource1.Name = "DataSet4";
            reportDataSource1.Value = this.ProductBindingSource;

            this.viewerInstance.LocalReport.DataSources.Add(reportDataSource1);
            this.viewerInstance.LocalReport.ReportEmbeddedResource = "YourReport.rdlc";
            this.viewerInstance.ZoomPercent = 95;
            this.windowsFormsHost1.Width = 680;

            ((System.ComponentModel.ISupportInitialize)(this.ProductBindingSource)).EndInit();
    }

唯一要留下的就是指定你想在报告中查看的对象。

private System.Windows.Forms.BindingSource ProductBindingSource;
        private void startReport()
        {
            YourClass item  = (YourClass)DataGridView.SelectedItem;
            this.ProductBindingSource.DataSource = item;

            this.viewerInstance.RefreshReport();
            this.viewerInstance.Refresh();
        }

哦,我的天!这个程序运行得真是太好了!感谢您结束了我的痛苦! - Gábor Birkás

1

几个月前,A正在开发类似的东西。然而,这里需要发布太多代码,但是看看这个已经完成的示例和源代码。 高级报告查看器 Codeproject


我可以再问你一个问题吗?这是我的报告的截图:链接 我如何将我的ObservableCollection中的数据传递到DataSet4的突出显示部分,最终传递到每个部分?谢谢! - Gábor Birkás

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接