如何在 WPF 项目中将 rdlc 文件添加到 ReportViewer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17709811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add rdlc file to ReportViewer in WPF projects
提问by Gábor Birkás
I've added a ReportViewerin a WPFapp via the XAMLdesigner of my main window and I'd like to add an existing rdlc file to it.
我已经通过主窗口的设计器ReportViewer在WPF应用程序中添加了一个XAML,我想向其中添加一个现有的 rdlc 文件。
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.
我希望我的报告查看器在启动时显示一个空的 rdlc 文件(没有参数),然后在从我的数据网格(绑定到一个 observablecollection)中选择一行时相应地更改其参数并显示填充的报告定义而不是空的.
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:
我将使用所选行作为命令参数和相关事件以及所有内容制作一个按钮,我只需要能够将数据传递给报告。我意识到这不是一个简单的问题,所以我会尽量简化:
- How to add an existing rdlc file to a ReportViewer (MVVM, WPF) ?
- I push a button -> relevant command gets the item from my observablecollection as parameter (a row in my datagrid) -> How to pass the data parts of this item to the unfilled (or overwrite if filled of course) parts of the report?
- 如何将现有的 rdlc 文件添加到 ReportViewer(MVVM、WPF)?
- 我按下一个按钮 -> 相关命令从我的 observablecollection 中获取项目作为参数(我的数据网格中的一行) -> 如何将此项目的数据部分传递给报告的未填充(或覆盖,当然如果已填充)部分?
I hope I've been clear. Thanks for the answer in advance!
我希望我已经清楚了。感谢您提前回答!
采纳答案by Labrinths
After you set up your initilizeMethod with correct path to the report and dataset name something like this.
在您使用正确的报告路径和数据集名称设置您的 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();
}
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();
}
回答by Labrinths
A was developing somethink like that a couple of months ago. However it's to much code to post here but have a look at this its completed sample with source Code. Advanced-Report-Viewver Codeproject
几个月前,A 正在发展类似的想法。然而,这里发布的代码太多了,但请查看它的完整示例和源代码。 高级报告查看器代码项目

