【问题标题】:How to add rdlc file to ReportViewer in WPF projects如何在 WPF 项目中将 rdlc 文件添加到 ReportViewer
【发布时间】:2013-07-16 14:20:44
【问题描述】:

我通过主窗口的XAML 设计器在WPF 应用程序中添加了ReportViewer,我想向其中添加现有的rdlc 文件。

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

我将使用所选行作为命令参数以及相关事件和所有内容制作一个按钮,我只需要能够将数据传递给报告。我意识到这不是一个简单的问题,所以我会尽量简化:

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

我希望我已经清楚了。感谢您提前回答!

【问题讨论】:

    标签: c# wpf mvvm reportviewer rdlc


    【解决方案1】:

    使用正确的报告路径和数据集名称设置 initilizeMethod 后,类似这样。

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

    【讨论】:

    • 天哪!奇迹般有效!谢谢你结束了我的痛苦!
    【解决方案2】:

    几个月前,A 正在开发类似的想法。 然而,在这里发布的代码太多了,但是看看它的完整示例和源代码。 Advanced-Report-Viewver Codeproject

    【讨论】:

    • 谢谢,这很有帮助!
    • 我可以再问你一个问题吗?这是我的报告的打印屏幕:link 如何将数据从我的ObservableCollection 传递到 DataSet4 的突出显示部分并最终传递到每个部分?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 2023-01-26
    • 1970-01-01
    • 2010-11-20
    • 2020-04-12
    • 2020-06-22
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多