Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

c# - How to add rdlc file to ReportViewer in WPF projects

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.

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. How to add an existing rdlc file to a ReportViewer (MVVM, WPF) ?
  2. 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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...