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.
- Download crystal report library and take reference of the DLL in your project.
- Use 'CrystalDecisions.Shared' namespace in your project.
- if you are facing any kind of issue for using reference of crystal report, visit this site.
- 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
- SAPBusinessObjects.WPF.Viewer.dll
- SAPBusinessObjects.WPF.ViewerShared.dll
- System.Windows.Controls.DataVisualization.Toolkit.dll
- Use above XAML and C# code in your aplication.
Thanks
Kailash Chandra Behera
Comments
Post a Comment