IntroductionYes 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. ImplementationFor the demonstration create new WPF project using Visual Studio 2008 and give name as "WPFReport". Take look followings figure to get it clear picture 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 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. 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. 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. Then you need to expand current connection tag, select your database connection then select the relevant table as shown by the following figure. Then click Next button to continue. There you need to select fields from selected tables to design report as shown by the following figure. Then click Next button. Now you have done necessary effort and finished the final step. As a result, you will get report like this. 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. And then navigate to .NET 3.0 Framework folders in your pc, Under the V3.0 folder there so many library from the collection you need select the "WindowsFormsHost". Look like following figure When you are done above steps correctly you will see the result like followings. 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. 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 |