如何在 WPF 项目中将 rdlc 文件添加到 ReportViewer [英] How to add rdlc file to ReportViewer in WPF projects

查看:28
本文介绍了如何在 WPF 项目中将 rdlc 文件添加到 ReportViewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've added a ReportViewer in a WPF app via the XAML designer of my main window and I'd like to add an existing rdlc file to it.

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

I'd like my reportviewer to show an empty rdlc file (without the parameters) on startup, and later upon selecting a row from my datagrid (bound to an observablecollection) change its parameters accordingly and show the filled report definition instead of the empty one.

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

I'll make a button with the selected row as commandparameter and the relevant events and everything, I just need to be able to pass data to the report. I realize it is not an easy question so I'll try to simplify:

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

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

I hope I've been clear. Thanks for the answer in advance!

推荐答案

在使用正确的报告路径和数据集名称设置 initilizeMethod 之后.

After you set up your initilizeMethod with correct path to the report and dataset name something like this.

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

唯一应该留下的是指定您想在报告中看到的对象.

Only thing that should be left is specifing the object you want to se in your report.

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

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

这篇关于如何在 WPF 项目中将 rdlc 文件添加到 ReportViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆