Answered by:
Using Report Viewer Control in WPF

Question
-
Hi
I need to use the Win forms Report Viewer control in a WPF application. I believe that I need to use the WindowsFormsHost control in the WPF application to host the report viewer. Also this would have to be done programmatically.
I have tried the solution on this post but the code does not compile. Not sure if anything is missing.
Has anyone had a chance to work on this?
Thanks
Nakul RingshiaMonday, January 19, 2009 7:32 AM
Answers
-
Hello Nakul,
The Win forms Report Viewer need some settings.
Sorry I didn't verify the sample in the given link.
Here is an example. We bind the DataSource to an object.
1. Define the demo datasource.using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WpfhostReportViewer { class BusinessObjects { } // Define the Business Object "Product" with two public properties // of simple datatypes. public class Product { private string m_name; private int m_price; public Product(string name, int price) { m_name = name; m_price = price; } public string Name { get { return m_name; } } public int Price { get { return m_price; } } } // Define Business Object "Merchant" that provides a // GetProducts method that returns a collection of // Product objects. public class Merchant { private List<Product> m_products; public Merchant() { m_products = new List<Product>(); m_products.Add(new Product("Pen", 25)); m_products.Add(new Product("Pencil", 30)); m_products.Add(new Product("Notebook", 15)); } public List<Product> GetProducts() { return m_products; } } }
Window XAML Page.
<Window x:Class="WpfhostReportViewer.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewer="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"> <Grid> <WindowsFormsHost Margin="12" Name="windowsFormsHost1"> <viewer:ReportViewer x:Name="viewerInstance" ZoomChange="viewerInstance_ZoomChange"> </viewer:ReportViewer> </WindowsFormsHost> </Grid> </Window>
Code for the main Window
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using Microsoft.ReportingServices; using Microsoft.Reporting; using Microsoft.Reporting.WinForms; using System.Data; namespace WpfhostReportViewer { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { private System.Windows.Forms.BindingSource ProductBindingSource; private System.ComponentModel.IContainer mform_components = null; private Merchant m_merchant = new Merchant(); public Window1() { InitializeComponent(); PrepareReport(); } private void PrepareReport() { 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 = "WpfhostReportViewer_Product"; reportDataSource1.Value = this.ProductBindingSource; this.viewerInstance.LocalReport.DataSources.Add(reportDataSource1); this.viewerInstance.LocalReport.ReportEmbeddedResource = "WpfhostReportViewer.MyReport.rdlc"; this.viewerInstance.Location = new System.Drawing.Point(89, 119); ((System.ComponentModel.ISupportInitialize)(this.ProductBindingSource)).EndInit(); } private void viewerInstance_ZoomChange(object sender, ZoomChangeEventArgs e) { } private void Window_Loaded(object sender, RoutedEventArgs e) { this.ProductBindingSource.DataSource = m_merchant.GetProducts(); this.viewerInstance.RefreshReport(); } } }
More:
1. 'WpfhostReportViewer' is the project name (wpf window project) and the name of generated assembly
2. MyReport.rdlc is a report contained in the project.
3. The sample is from Walkthrough: Using a Business Object Data Source with the ReportViewer Windows Forms Control in Local Processing Mode we can follow the steps there to create a Report.
Generally the we can follow the usage of the Report Viewer in Windows Form programs.
If you have any question please let me know.
Thanks.
Please mark the replies as answers if they help and unmark them if they provide no help- Marked as answer by Hua Chen Tuesday, January 27, 2009 2:38 AM
Tuesday, January 20, 2009 9:49 AM -
I discovered that this works:
this.viewerInstance.LocalReport.ReportPath = "../../MyReport.rdlc";
instead of
this.viewerInstance.LocalReport.Repo rtEmbedded Resource = "WpfhostReportViewer.MyRep ort.rdlc"; - Marked as answer by Hua Chen Monday, March 2, 2009 8:44 AM
Tuesday, February 24, 2009 5:41 PM
All replies
-
Hello Nakul,
The Win forms Report Viewer need some settings.
Sorry I didn't verify the sample in the given link.
Here is an example. We bind the DataSource to an object.
1. Define the demo datasource.using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WpfhostReportViewer { class BusinessObjects { } // Define the Business Object "Product" with two public properties // of simple datatypes. public class Product { private string m_name; private int m_price; public Product(string name, int price) { m_name = name; m_price = price; } public string Name { get { return m_name; } } public int Price { get { return m_price; } } } // Define Business Object "Merchant" that provides a // GetProducts method that returns a collection of // Product objects. public class Merchant { private List<Product> m_products; public Merchant() { m_products = new List<Product>(); m_products.Add(new Product("Pen", 25)); m_products.Add(new Product("Pencil", 30)); m_products.Add(new Product("Notebook", 15)); } public List<Product> GetProducts() { return m_products; } } }
Window XAML Page.
<Window x:Class="WpfhostReportViewer.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewer="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"> <Grid> <WindowsFormsHost Margin="12" Name="windowsFormsHost1"> <viewer:ReportViewer x:Name="viewerInstance" ZoomChange="viewerInstance_ZoomChange"> </viewer:ReportViewer> </WindowsFormsHost> </Grid> </Window>
Code for the main Window
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using Microsoft.ReportingServices; using Microsoft.Reporting; using Microsoft.Reporting.WinForms; using System.Data; namespace WpfhostReportViewer { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { private System.Windows.Forms.BindingSource ProductBindingSource; private System.ComponentModel.IContainer mform_components = null; private Merchant m_merchant = new Merchant(); public Window1() { InitializeComponent(); PrepareReport(); } private void PrepareReport() { 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 = "WpfhostReportViewer_Product"; reportDataSource1.Value = this.ProductBindingSource; this.viewerInstance.LocalReport.DataSources.Add(reportDataSource1); this.viewerInstance.LocalReport.ReportEmbeddedResource = "WpfhostReportViewer.MyReport.rdlc"; this.viewerInstance.Location = new System.Drawing.Point(89, 119); ((System.ComponentModel.ISupportInitialize)(this.ProductBindingSource)).EndInit(); } private void viewerInstance_ZoomChange(object sender, ZoomChangeEventArgs e) { } private void Window_Loaded(object sender, RoutedEventArgs e) { this.ProductBindingSource.DataSource = m_merchant.GetProducts(); this.viewerInstance.RefreshReport(); } } }
More:
1. 'WpfhostReportViewer' is the project name (wpf window project) and the name of generated assembly
2. MyReport.rdlc is a report contained in the project.
3. The sample is from Walkthrough: Using a Business Object Data Source with the ReportViewer Windows Forms Control in Local Processing Mode we can follow the steps there to create a Report.
Generally the we can follow the usage of the Report Viewer in Windows Form programs.
If you have any question please let me know.
Thanks.
Please mark the replies as answers if they help and unmark them if they provide no help- Marked as answer by Hua Chen Tuesday, January 27, 2009 2:38 AM
Tuesday, January 20, 2009 9:49 AM -
I followed your code and directions exactly (I think) but cannot display the contents of the report. It shows the shell, with the ReportViewer toolbar, but tells me
"An error occurred during local report processing. The report definition for report WpfhostReportViewer.MyReport.rdlc has not been specified."
What am I missing?
I created WpfhostReportViewer.MyReport.rdlc as you suggested at Walkthrough: Using a Business Object Data Source with the ReportViewer Windows Forms Control in Local Processing Mode using a WinForms app, which displays MyReport.rdlc just fine.(I tried to send the XML for MyReport.rdlc but it's too long. Ditto with the WPF app code, but it is the same as in your post.)Will I have to call up a separate WinForms file to display the ReportViewer? If so, how is that done?
Thanks!
Dave McMullen
(I am creating a separate post(s) with the WPF app code.)Sunday, February 15, 2009 11:54 PM -
Anyone listening?Wednesday, February 18, 2009 10:43 PM
-
I discovered that this works:
this.viewerInstance.LocalReport.ReportPath = "../../MyReport.rdlc";
instead of
this.viewerInstance.LocalReport.Repo rtEmbedded Resource = "WpfhostReportViewer.MyRep ort.rdlc"; - Marked as answer by Hua Chen Monday, March 2, 2009 8:44 AM
Tuesday, February 24, 2009 5:41 PM -
Hi Chen,
I have followed your code and instead of List collection I have bind the Dataset as a datasource, the datasource cannot be bind in the viewer.
Please advice.
I have done the following approach to view reports dynamically in the report viewer
1. I have created a rdlc file using a dummy schema.
2. I have followed your code to initialize the report viewer.
3. Instead of List collection I have bind the dataset as a datasource.
Please find the attached code.
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using Microsoft.ReportingServices; using Microsoft.Reporting; using Microsoft.Reporting.WinForms; using System.Data; using HoneyWell.PMTool.BusinessFacade; using HoneyWell.PMTool.Logging; namespace HoneyWell.PMTool.WPFWindowsClient { /// <summary> /// Interaction logic for TestPage.xaml /// </summary> public partial class TestPage : Page { private System.Windows.Forms.BindingSource ProductBindingSource; HoneyWell.PMTool.BusinessFacade.Report objReport; public TestPage() { InitializeComponent(); PrepareReport(); } /// <summary> /// /// </summary> private void PrepareReport() { Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource(); this.ProductBindingSource = new System.Windows.Forms.BindingSource(); ((System.ComponentModel.ISupportInitialize)(this.ProductBindingSource)).BeginInit(); this.viewerInstance.LocalReport.ReportPath = @"C:\Documents and Settings\E446542\e446542_view\PMTool\SourceCode\WPFPMTool\Preventative Maintenance Tool\HoneyWell.PMTool.Report\Completed Task Report.rdlc"; reportDataSource1.Value = this.ProductBindingSource; this.viewerInstance.LocalReport.DataSources.Add(reportDataSource1); this.viewerInstance.LocalReport.ReportEmbeddedResource = "HoneyWell.PMTool.Report.Completed Task Report.rdlc"; this.viewerInstance.Location = new System.Drawing.Point(89, 119); ((System.ComponentModel.ISupportInitialize)(this.ProductBindingSource)).EndInit(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void viewerInstance_ZoomChange(object sender, ZoomChangeEventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Loaded(object sender, RoutedEventArgs e) { objReport = new HoneyWell.PMTool.BusinessFacade.Report(); DataSet ds = objReport.GetCompletedTaskInfo(); ds.DataSetName = "Completed_Task_List"; this.ProductBindingSource.DataSource = objReport.GetCompletedTaskInfo(); this.viewerInstance.RefreshReport(); } } }
Thanks,
Malarvannan MMonday, June 1, 2009 11:00 AM -
Maybe a useful addition about referencing embedded resources: if you open the generated .exe or .dll in ildasm, and open the manifest file, you can lookup how to reference an embedded resource.
You would find an embedded resource entry like this for example to a report definition file:
.mresource public Indo.Indonet9.Aplicacion.Receta.rdlc
{
// Offset: 0x001A9A7F Length: 0x0000074D
}
and then you would know to refer the report like:
Me.viewerInstance.LocalReport.ReportEmbeddedResource = "Indo.Indonet9.Aplicacion.Receta.rdlc"
Tuesday, February 2, 2010 2:34 PM -
Did any one get this code to work. I am getting a blank screen with the View shell (but no data and no error messages).
Does the viewer actually work in WPF?
I am using VS 2010 with framework 4.0
Any help/advise is appreciated.
n.o.
Saturday, July 24, 2010 1:55 AM -
I spent over a week on the Report View in a WPF application. The results aren't very promising.
I can now show a report but only a canned report.
This wasn't of course what I was after. I needed to have a reporting system that allows dynamic queries. There is a Dynamic.cs file on the codeplex that you can add to your project and it is supposed to help with generating run-time queries. But there are 2 problems; The Dynamic.cs take it upon itself to syntax check your query. I faced too many issues in convincing the Dynamic.cs that my query was okay. The final result was to have something like Select someFixedFields from someFixedtables where anotherFixedfiled = AVariableValue. I could ONLY change the VariableValue at runtime and get a different set of data at each run. Dynamic? yes! but not dynamic enough for me...
The 2nd and major problem is of course the actual limitation of the ReportViewer. ReportView uses a .rdlc file and .rdlc needs to be created based on a specific dataset. I could not find an easy way to change the dataset at runtime. It is feasible to create a .rdlc file (which is an xml file containing the dataset) at run time and use it to generate your report in the reportviewer. but this path sounded too time consuming and therefore too risky.
My present solution is to pull data based on a completely dynamic sql query and fill out a data table. Then use a data grid and dump the data from the table in the data grid a bunch of records at a time.
That works pretty nicely... The next step is the printing of the a multi-page report...
Hope these guidelines save someone some times.
n.o.
Monday, August 2, 2010 9:56 PM -
I also get the message "A data source instance has not been supplied for the data source 'DataSet1' ". If it is impossible to run reportview in wpf this article is totally missleading and should be removed!Thursday, December 2, 2010 8:18 AM
-
I actually got it to work now, it was this row that ties datasource to what the .rdlc file has named dataset had to match:
reportDataSource1.Name = "DataSet1" ;
Thursday, December 2, 2010 10:34 AM -
you can use something like this too. and if you put [ReportDataSource and ListReportParamiter] as parameter you can get dynamic report.
private void PrepareReport() { this.viewerInstance.LocalReport.ReportPath = @"C:\Programs\VS2010\Report1.rdlc"; ReportDataSource RDS = new ReportDataSource("DataSet1",ListCustomer); ReportParameter rb = new ReportParameter("rp01","test",true); List<ReportParameter> ListReportParameter = new List<ReportParameter>(); ListReportParameter.Add(rb); this.viewerInstance.LocalReport.SetParameters(ListReportParameter); this.viewerInstance.LocalReport.DataSources.Add(RDS); this.viewerInstance.RefreshReport(); }
Tuesday, March 15, 2011 4:17 PM -
Created report as Waleed mentioned. Can any one suggest how to display ms sql - table- field of type image and content is a business object in report viewer?
Thanks in advance
ursri
Tuesday, March 22, 2011 12:39 PM