Incorporate Crystal Reports in WPF using C#

No.of Views12091
Bookmarked2 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:47
Tag : Crystal Reports , How to
Incorporate Crystal Reports in WPF using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Yes we are all know create crystal report in windows form application and ASP.NET application.but in the windows presentation foundation it's little different, that means crystal report viewer doesn't have designer support and so you could ask a question. We can't create report in WPF applications? If yes since how we could create report in WPF application?

We can create report using crystal report in WPF application. But to this we need use the "WindowsFormsHost" object to add Crystal report reviewer within the WPF window. This is come with .NET 3.0 Framework.

In this article I'm going to explain whole details to create report using crystal report and render to front in WPF application. 

Implementation

For the demonstration create new WPF project using Visual Studio 2008 and give name as "WPFReport". Take look followings figure to get it clear picture

Image Loading

Change file window name as "ReportViewerView" and then click add button to add to the project. Now we need to create report and design report with database. To this right click in your project and then select Reporting nodes in tree view under the Visual C#. Then select crystal report. Look like following figure

Image Loading

Here change the report file name is CryReportDemo.rpt. And then click add button to add to the project. It does bring a report design wizard.

Image Loading

Keep the Standard report format and click OK button. Then you need to create the database connection to access tables within the database. Once you click OK button, system gives a window to select the connection type as shown below.

Image Loading

This demonstration report is to work with SQL Server 2005 database. So you need to select SQL native client as provider (As above figure shows).

Then click Next button, to continue report creation process. Then you need to assign details of SQL Server and user credential to access the database from the report.

Image Loading

Then you need to expand current connection tag, select your database connection then select the relevant table as shown by the following figure.

Image Loading

Then click Next button to continue. There you need to select fields from selected tables to design report as shown by the following figure.

Image Loading

Then click Next button. Now you have done necessary effort and finished the final step. As a result, you will get report like this.

Image Loading

Report is design part is finished. now we need load this report in WPF window. To this we can't add report viewer to WPF directly, there is no designer support in current version. As I mentioned in introduction of this article top we need   use the "WindowsFormHost" object to add windows Forms control within the WPF application.

To this right click on your project, then select Add Reference menu.

Image Loading

And then navigate to .NET 3.0 Framework folders in your pc,

Image Loading

Under the V3.0 folder there so many library from the collection you need select the "WindowsFormsHost". Look like following figure

Image Loading

 

When you are done above steps correctly you will see the result like followings.

Image Loading

Note: Without this we can't access the "WindowsFormsHost" object in your class or anywhere in project.

Now we have form host object, let's add the windows form host object under the grid in XAML. When you add form host object in design time using XAML its look like followings.

XAML

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ReportViewerView" Height="300" Width="300" Loaded="Window_Loaded">

Now we can add any windows form control within the "windowsFormHost" as children. According this rule we will place the report viewer also into the "windowsFormsHost" as children dynamically.

Let's write inline code in c#.

//Code RRaveen (vrrave[at]codegain.com)//Offical site: http://codegain.com//This program is free software: you can redistribute it and/or modify//it under the terms of the GNU Lesser General Public License as published by//the Free Software Foundation, either version 3 of the License, or//(at your option) any later version.//This program is distributed in the hope that it will be useful,//but WITHOUT ANY WARRANTY; without even the implied warranty of//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the//GNU Lesser General Public License for more details.//You should have received a copy of the GNU Lesser General Public License//along with this program.  If not, see .using System;using System.Collections.Generic;using System.Linq;using System.Text;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.Shapes;using CrystalDecisions.Windows.Forms;using CrystalDecisions.CrystalReports.Engine;namespace CG.CS.WPFDemos.Report

{////// Interaction logic for ReportViewerView.xaml///public partial class ReportViewerView : Window

{CrystalReportViewer cryreportviewer = null;public ReportViewerView(){InitializeComponent();// create new crystalReport viewercryreportviewer = new CrystalReportViewer();cryreportviewer.DisplayGroupTree = false;// add crystl report view as child control  to windows forms host object.reportViewer.Child = cryreportviewer;}private void Window_Loaded(object sender, RoutedEventArgs e){if (reportViewer == null){cryreportviewer = new CrystalReportViewer();reportViewer.Child = cryreportviewer;}// create new report documentReportDocument report = new ReportDocument();// load report to documentreport.Load("Report/CryReportDemo.rpt");// set database logon inforation to report document object.report.SetDatabaseLogon("username", "pass", @"TGS-LT-01\EXPRESS", "dbname");//finally set report object to viewer.cryreportviewer.ReportSource = report;}}}

Here we need take look ReportViewerView constrcutor code.Before that just declare crystal report viewer control.

CrystalReportViewer cryreportviewer = null;

Then write create isntance to report viewer and then add into the windows forms host children like ,

cryreportviewer = new CrystalReportViewer();cryreportviewer.DisplayGroupTree = false;// add crystl report view as child control  to windows forms host object.reportViewer.Child = cryreportviewer;

In the form load just create report document and load design file, and then set database logon information to report object, finally set report object to viewer.

// create new report documentReportDocument report = new ReportDocument();// load report to documentreport.Load("Report/CryReportDemo.rpt");// set database logon inforation to report document object.report.SetDatabaseLogon("username", "pass", @"TGS-LT-01\EXPRESS", "dbname");//finally set report object to viewer.cryreportviewer.ReportSource = report;

Now build application, if there is no compli error , just press F5 to bring mafic in view. It's look like followings.

 

Image Loading

 

Conclusion

This article explain about create report within Windows Presentation Foundation application by dynamic code.and I hope its save most of the developers who are working with WPF.

Sample Project Source

Download source files -102 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:Pravesh SinghDate Of Posted:3/15/2012 6:15:02 AM
Hosting Crystal Report in Windows Form
I was reading your article and I would like to appreciate you for making it very simple and understandable. This article gives me a basic idea of Hosting Crystal Report in Windows Form. Check these articles too.... http://msdn.microsoft.com/en-us/library/aa665783%28v=vs.71%29.aspx http://www.mindstick.com/Articles/09e06499-6f64-4fc6-bc84-8cc40a72103e/?Hosting%20Crystal%20Report%20in%20Windows%20Form
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top