Crystal Report Viewer in WPF: Part 2



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...