Use Crystal Report in WPF

Kailash Chandra Behera | Friday, April 06, 2018

Introduction

By defult the crystal report software is not part of wpf controls, this following blog guides and provides code snip to use crystals report for WPF in C#.

Use Crystal Report in WPF

Use Crystal Report in WPF

Getting Started

Let’s say you have already developed crystals report(rpt file) using rpt file viewer like above given Image and you want use it in your WPF application for rpt viewer at runtime.

Earlier the crystals report was directly supported in win form and Asp.Net and the rpt viewer were listed in the toolbox, but In WPF you will not get it in the toolbox you need to manually add crystal rpt viewer in XAML by using the namespace.

The behind code is almost the same as the windows used or ASP.Net. Here we will see the both CSharp code and XAML code to use crystal report, see the below code to use crystal report.

XAML Code Snip: For Crystals Report Viewer

 <Window x:Class="CrystalReportDemo.MainWindow"  
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
     xmlns:local="clr-namespace:CrystalReportDemo"  
     mc:Ignorable="d"  
     xmlns:CR="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"  
     Title="MainWindow" Height="350" Width="525">  
   <Grid>  
     <Grid.RowDefinitions>  
       <RowDefinition Height="30"/>  
       <RowDefinition Height="*"/>  
     </Grid.RowDefinitions>  
     <Button Content="View Report" HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>  
     <CR:CrystalReportsViewer Grid.Row="1" Name="irRapportViewer" ToggleSidePanel="None" Margin="0,35,0,0"></CR:CrystalReportsViewer>  
   </Grid>  
 </Window>  

C# Code Snip: Loading Crystal Report

 using CrystalDecisions.CrystalReports.Engine;  
 using CrystalDecisions.Shared;  
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 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;  
 namespace CrystalReportDemo  
 {  
   /// <summary>  
   /// Interaction logic for MainWindow.xaml  
   /// </summary>  
   public partial class MainWindow : Window  
   {  
     public MainWindow()  
     {  
       InitializeComponent();  
     }  
     private void Button_Click(object sender, RoutedEventArgs e)  
     {  
       //irRapportViewer.ViewerCore.RefreshReport();  
       ParameterFields paramFields = new ParameterFields();  
       ParameterField pfItemYr = new ParameterField();  
       pfItemYr.ParameterFieldName = "@No";  
       ParameterDiscreteValue dcItemYr = new ParameterDiscreteValue();  
       dcItemYr.Value = 1;  
       pfItemYr.CurrentValues.Add(dcItemYr);  
       paramFields.Add(pfItemYr);  
       ReportDocument rptDoc = new ReportDocument();  
       rptDoc.Load(@"C:\Users\Imixadmin\source\repos\CrystalReportDemo\CrystalReportDemo\CrystalReport.rpt"); 
       rptDoc.SetDatabaseLogon("UserName", "Password", "SQL Server Name", "Database Name"); 
       irRapportViewer.ViewerCore.ReportSource = rptDoc;  
       irRapportViewer.ViewerCore.ParameterFieldInfo = paramFields;
     }  
   }  
 }  
Both codes described how to place report viewer in XAML and load rpt file using behind code. The below steps to provides a complete guideline use crystal report in WPF. The steps are very simple, just follow the below steps.

Steps To Use Crystals Report In WPF.

  1. Download crystal report library(crystal reports download) and take reference of the DLL in your project.
  2. Open your WPF application where you want to implement crystal report.
  3. Just testing purpose add a new Windows control to you project and name it "testingcrystals".
  4. Open the testingcrystals.xaml file and take reference of CrystalDecisions.Shared namespace like below.
     xmlns:CR="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"   
    
  5. Then copy the XAML from to and replace with the grid control in the testingcrystals.xaml file.
  6. Copy the button event from the above C# code snippet and paste it in your testingcrystals.xaml.cs file. change the code as per your requirement.
  7. if you are facing any kind of problem, visit this site for reference of crystal report .
  8. Take reference of below listed DLLs , you will find these DLLs from this path : C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet
    1. SAPBusinessObjects.WPF.Viewer.dll
    2. SAPBusinessObjects.WPF.ViewerShared.dll
    3. System.Windows.Controls.DataVisualization.Toolkit.dll
  9. Now you are done with implementation of crystals report in C# WPF application.

Summary

In the above of this article, we say how to implement the crystals report in C# WPF application. I hope you have enjoyed it a log.

Thanks