SIGN UP MEMBER LOGIN:    
ARTICLE

Crystal Report Viewer in WPF: Part 2

Posted by Nipun Tomar Articles | WPF with C# 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
share this article :
post comment
 

This example is awesome for WPF programmers. But I have a problem about that. This print button is not working for me. Can you help me to that problem?

Posted by hemal asanka Apr 18, 2012

Hello all, I am not sure if anyone is answering this or not. I see your "got it" replies but that is it. Thanks for your interest. Gary

Posted by Gary McGeehon Jan 23, 2012

You got it Good :)

Posted by Nipun Tomar Jan 22, 2012

sorry.got it

Posted by atanu shome Jan 20, 2012

WpfApplication4Entities1 what is it? getting an error

Posted by atanu shome Jan 20, 2012
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor