Export (0) Print
Expand All

Walkthrough: Using ReportViewer in a WPF Application

To use the ReportViewer control in a WPF application, you need to add the ReportViewer assembly to your project and programmatically configure it. WPF supports Windows Forms controls, but the ReportViewer control does not provide any automatic configuration for WPF as it does in a Windows Forms application.

In a WPF application, you host the ReportViewer control inside a WindowsFormsHost control. The WindowsFormsHost control requires full-trust to the calling code, which is the trust level of a WPF client application in Windows. In a WPF browser application, the application runs in partial trust by default, and you must do the following (for more information, see WPF XAML Browser Applications Overview):

  • Configure the WPF browser application to run in full trust.

  • Make sure the deployment Web site is in either the Local Intranet or the Trusted Sites zone in the client browser.

In this walkthrough, you will create a WPF application that displays a local report using the AdventureWorks2008R2 sample database.

To Use ReportViewer in a WPF Application

  1. In the File menu, point to New and select Project.

  2. In the New Project dialog, select the WPF Application project type, name your project, and click OK.

  3. From the Toolbox, drag a WindowsFormsHost control onto the design surface for MainWindow.xaml. This adds the assemblies needed by WindowsFormstHost to your project.

  4. In Solution Explorer, right-click your project and select Add Reference.

  5. In the .NET tab of the Add Reference dialog log, select the Microsoft.ReportViewer.WinForms assembly, and click OK.

  6. In XAML view, add the following highlighted lines:

    <Window x:Class="WpfReportApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" 
            Title="MainWindow" Height="350" Width="525">
        <Grid>
          <WindowsFormsHost> 
            <rv:ReportViewer x:Name="_reportViewer"/> 
          </WindowsFormsHost>
        </Grid>
    </Window>
    

    This creates an instance of ReportViewer named _reportViewer in the XAML page. Next, you will create an RDLC report that you will display in the ReportViewer control.

  7. In Solution Explorer, right-click your project, point to Add, and select New Item.

  8. In the Add New Item dialog box, select Report Wizard, keep the default name, and click Add. This launches the Report Wizard with the Data Source Configuration Wizard.

  9. In the Data Source Configuration Wizard, select Database, click Next, select Dataset, and click Next again.

  10. In the Choose Your Data Connection page, click New Connection. If you see the Choose Data Source dialog box, select Microsoft SQL Server and click Continue.

  11. In the Server name box, type the server name that hosts the AdventureWorks2008R2 database, then in Select or enter a database name, select AdventureWorks2008R2, and then click OK.

  12. Click Next twice.

  13. In the Choose Your Database Objects page, expand the Tables node, select the checkbox for the SalesOrderDetail (Sales) table, and click Finish.

    A DataSet object called AdventureWorks2008R2DataSet is now created in your project.

  14. In the Report Wizard, click Next. In the Arrange Fields page, drag all the available fields to the Values pane. This creates a simple tabular table for displaying the sample data. Then, click Next three times to close the Report Wizard.

    Next, you add code to point ReportViewer to the new report you created and to add data from AdventureWorks2008R2DataSet to ReportViewer.

  15. Open MainWindow.xaml.cs, and add the highlighted line to the MainWindow() constructor:

    public MainWindow()
    {
        InitializeComponent();
        _reportViewer.Load += ReportViewer_Load;
    }
    
  16. Add the following code below the MainWindow() constructor in the class definition:

    private bool _isReportViewerLoaded;
    
    private void ReportViewer_Load(object sender, EventArgs e)
    {
        if (!_isReportViewerLoaded)
        {
            Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
            AdventureWorks2008R2DataSet dataset = new AdventureWorks2008R2DataSet();
    
            dataset.BeginInit();
    
            reportDataSource1.Name = "DataSet1"; //Name of the report dataset in our .RDLC file
            reportDataSource1.Value = dataset.SalesOrderDetail;
            this._reportViewer.LocalReport.DataSources.Add(reportDataSource1);
            this._reportViewer.LocalReport.ReportEmbeddedResource = "<VSProjectName>.Report1.rdlc";
    
            dataset.EndInit();
    
            //fill data into adventureWorksDataSet
            AdventureWorks2008R2DataSetTableAdapters.SalesOrderDetailTableAdapter salesOrderDetailTableAdapter = new AdventureWorks2008R2DataSetTableAdapters.SalesOrderDetailTableAdapter();
            salesOrderDetailTableAdapter.ClearBeforeFill = true;
            salesOrderDetailTableAdapter.Fill(dataset.SalesOrderDetail);
    
            _reportViewer.RefreshReport();
    
            _isReportViewerLoaded = true;
        }
    }
    
  17. In the Debug menu, select Start Debugging to run the WPF application.

Show:
© 2014 Microsoft