Skip to main content

Use of Crystal Report in WPF

Introduction

This following blog describes how to code and the necessary steps to do for using crystal report in WPF. It provides code snippet for using crystal report.

Getting Started

Let’s say you have already develop crystal report given below Image and you want use it in your WPF application.


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

For using crystal report in WPF, the code behind code is almost same as win form or asp. Net . This blog provides both CSharp code and XAML code to use of crystal report, see the below code to use crystal report.

XAML Code For Using Crystal Report Viewer in Windows

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

CSharp Code for 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;
     }  
   }  
 }  
The above code follows below steps to use crystal report in WPF. The steps are very simple to use crystal report in WPF, just follow the below steps to know how crystal report can be used in WPF.

Steps To Use Crystal Report In WPF.

  1. Download crystal report library and take reference of the DLL in your project.
  2. Use 'CrystalDecisions.Shared' namespace in your project.
  3. if you are facing any kind of issue for using reference of crystal report, visit this site.
  4. 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
  5. Use above XAML and C# code in your aplication.

Thanks
Kailash Chandra Behera

Comments

Popular posts from this blog

Generate QR Code in WPF

Introduction In my previous two blogs, we have discussed how to display generate and display various barcodes on the web page. In this blog, we are going to demonstrate how to Generate QR Code in the WPF application. Getting Started Here in the demonstration Generate QR Code in WPF , will generate QR code using third party library as there is not inbuilt library provided by Microsoft to generate QR code and will display in WPF image control, In the below, we will see the steps to display QR Code. As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate QR code, I have taken the help of the ZXing library which is a third party free library and available Nuget. This library provides various options to generate barcodes and QR Code from the user-friendly text. Generate QR Code in WPF Here are the steps to generate QR code and let's follow the steps to complete demonstrations. Demonstration:- Generate QR Code Open visual stud

Creating Application in Prism

Introduction This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version. Microsoft.Practices.Prism.dll. This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events. Microsoft.Practices.Prism.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class

WPF Binding

Introduction This blog describes WPF Binding and the measure elements exist with binding with XAML example in XAML and code-behind. Getting Started Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well. XAML Example <TextBox x:Name="sourceText" Grid.Row="0" /> <Button Grid.Row="1" Content="{Binding ElementName=sourceText, Path=Text}"/> Code Example Button btn=new Button(); Binding binding = new Binding("Text"); binding.Source = sourceText; btn.SetBinding(Button.ContentProperty, binding); In WPF binding has some measure properties or elements that we are using while developing application or projects, here we are going to d