ARTICLE

Crystal Report Viewer in WPF: Part 2

Posted by Nipun Tomar Articles | WPF December 27, 2011
Reports has been the reporting tool of choice included with Visual Studio and here you learn how to add Crystal report in WPF application.
Reader Level:


In previous article as we discussed about the adding Crystal report in WPF application and complete the process till creation of all the Columns, now this is the continues part and now we start from here:

  1. This time also you have to once more Add a New Item to your project and that will be the Crystal Report which is in Reporting template, select it and click Add.

    reportview18.gif

    reportview19.gif

  2. "Crystal Report Gallery" will be there now, Select "As A Blank Report" option and click the OK button.

    reportview20.gif

  3. You see, the report will appear in your design mode. Right click on the Database Fields node in the Field Explorer and select "Database Expert".

    reportview21.gif

  4. Expand the "Create New Connection node".

    reportview22.gif

  5. Again, Expand "ADO.NET (XML)" node which will display a dialog box in which we have to select the "DataSet1.xsd" file.

    reportview23.gif

  6. Now, Click on the browse button of "File Path" and select the DataSet1.xsd file to open. And after select the file you will be able to see the full path of your file in the File Path. Then, Click on the Finish button.

    reportview24.gif

    reportview25.gif

  7. The DataTable1 node will appear now. Click on the > button to move this node to the Selected Tables list and click OK.

    reportview26.gif

    reportview26a.gif

    reportview27.gif

  8. The DataTable1 node will appear under the "Database Fields" node in the Field Explorer window. Expand it to see.

    reportview28.gif       reportview29.gif

  9. Drag each field to the details section in the Crystal Report file. And Save it.

    reportview30.gif
  10. Add the following two assemblies/ dll files to your project:

    • SAPBusinessObjects.WPF.Viewer.dll
    • SAPBusinessObjects.WPF.ViewerShared.dll

      reportview31.gif

      reportview32.gif
       
  11. Also add these highlighted lines to your code:

    MainWindow.xaml:


    <
    Window x:Class="WpfCrystalReport.MainWindow"
            xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

            xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:my
    ="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=

                                                                                   SAPBusinessObjects.WPF.Viewer"

            Title="MainWindow" Height="350" Width="525">
       
    <Grid>
           
    <my:CrystalReportsViewer HorizontalAlignment="Left" Name="crystalReportsViewer1"
           
                           
      VerticalAlignment="Top" Height="500" Width="500" />
       
    </Grid>
    </
    Window>

  12. Use the following two Statements in your MainWindow.xaml.cs:

       
    using CrystalDecisions.CrystalReports.Engine;
       
    using CrystalDecisions.Shared;

    And add the following code:

        namespace WpfCrystalReport
        {

            ///
    <summary>

            ///
    Interaction logic for MainWindow.xaml

            ///
    </summary>

            public partial class MainWindow :
    Window

            {

                public MainWindow()

                {

                    InitializeComponent();

                }
     
                private void Window_Loaded(object sender, RoutedEventArgs e)

                {

                   ReportDocument report = new ReportDocument();

                   report.Load("../../CrystalReport1.rpt");
     
                   using (WpfApplication4Entities1 db = new WpfApplication4Entities1())

                   {

                      report.SetDataSource(from c in db.Clients

                      select new { c.CustomerName, c.Address, c.EmailId, c.PassportNumber});

                   }

                   crystalReportsViewer1.ViewerCore.ReportSource = report;

                }

              }
        }

  13. Lastly hit F5 to run your application and the result will be:

    reportview33.gif

Happy Learning...

Login to add your contents and source code to this article
post comment
     

Hello, could you please translate the c# code to vb.net or atleast try to comment on it,the article is very useful but the code confuses me.

Posted by Gee Chaxy Mar 28, 2013

Hello, Thanks for this tuto. I have this error: clr-namespace:SAPBusinessObjects.WPF.Viewer; assembly=SAPBusinessObjects.WPF.Viewer' mapping URI is not valid. Please, help me.

Posted by Aboup Lah Mar 12, 2013

Hello can you posted or upload a sample project , because i have problem in report.load("Crystalreport1.rpt"); my Email : sunn789@gmail.com

Posted by suni sunn Jan 24, 2013

I got This Error the tag'CrystalReportsViewer' does not exist in XML namespace 'clr-namespace:SAPBusinessObjects.WPF.Viewer; assemebly=SAPBusinessObject.WPF.Viewer'.

Posted by praful Dangre Dec 11, 2012

It's perfect article for beginners.......... Thanks

Posted by Shivam Bhadja Nov 29, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.