Using CrystalReportViewer to Display Crystal Reports in .Net

I explained in my previous article how to use Crystal Report Viewer to see Crystal Reports in an application. In this article you will learn about Crystal Report Viewer in more detail.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 33
October 07, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

First of all, having Crystal Reports along with Visual Studio .Net is a great advantage. It makes certain tasks easier for developers. Along with Crystal Reports, Visual Studio .Net provides various tools which help developers build applications faster. Crystal Report Viewer is one of the tools available in Visual Studio to help you bind Crystal Reports to your Windows forms and view reports as part of a Windows application.

There are two Crystal Report Viewers. One is Crystal Report Viewer for Windows forms and one is Crystal Report Viewer for web forms. We will focus on the former in this article. 

Let's see how to bind a report to Crystal Report Viewer.

  • Create any Crystal Report in your Windows application. Please refer to my previous article on creating summary Crystal Reports for more information.

  • Create a new Windows form. Go to Project Menu -> Add Windows Form.

  • Adjust the form size as per your Crystal Report size.

  • Crystal Report Viewer is a control available in the tool box with Visual Studio.net.



  • Add this control to your Windows form.

  • Resize the control (Crystal Report Viewer) so that it will fill the entire form. To do this, right click on Crystal Report Viewer and set its Dock Property to Fill. Select the drop down box and click on the middle square to set the property to Fill.



Binding with the Report Source Property

CrystalReportViewer is a class that belongs to the CrystalDecisions.windows.Forms namespace.

  • Bind Crystal Report to CrystalReportViewer.

We can bind Crystal Report to Viewer in several different ways.

  • We can set the Report Source Property to the desired report within the application or to the desired object within or outside application. Right click on CrystalReportViewer and go to properties; otherwise, simply select Viewer in the property window. Set the "ReportSource" Property by clicking on the drop down menu and selecting the desired Crystal Report from the application.



This will simply bind the report to a viewer.

  • You can add the ReportDocument component to your form by double clicking the ReportDocument component in the Crystal Reports section of the tool box.



Click on the drop down box and select the desired report from the application.

Binding with Code

  • You can also bind the report to CrystalReportViewer at run time programmatically.


The following lines of code can be added to the form's load event to bind the Crystal Report to the Viewer.

CrystalReportViewer1.ReportSource = "C:CrystalReportViewerTestCrystalReport1.rpt"


In the above code, "CrystalReportViewer1" is the name of the viewer object, "ReportSource" is the property to be set at run time for the Viewer. "CrystalReportViewerTest" is the name of the Windows application, and "CrystalReport1.rpt" is the name of the report already created in the Windows application.

Binding Crystal Reports at runtime is very beneficial, as we don't have to create forms for each and every Crystal Report in an application. We can simply create one form and bind the required report dynamically when necessary.

Sometimes we have multiple reports in an application, and we need to display a particular report depending upon the user's selection. In such cases, to create Crystal Reports dynamically, we use this type of binding.

Once you have bound your report to the viewer, you should run your application with the start form as the form to which the report is bound. You will see you report, which should look something like this: 



You can see a tool bar at the top of the report with different buttons.

Toolbar Functions

We can manage the functionality of each button programmatically by using the methods associated with it. The buttons on the bar are in the following order, with the listed functionality. 

  • Export Report: This method is used to export the report.

  • Print Report: This method is used to print the report.

  • Refresh: This method lets you refresh the report programmatically. 

  • Toggle Group Tree: This method is used to show the group tree.

  • Go to First Page: Use this method to go to the first page of the report.

  • Go to Previous Page: Use this method to go to the previous page of the report.

  • Go to Next Page: Use this method to go to the next page of the report.

  • Go to Last Page: This method takes you to the last page of the report.

  • Go to Page: This method is used to go to nth page of the report by specifying the desired page number.

  • Close Current View: This closes the current view of the report

  • Find Text: This particular method is widely used programmatically to find particular text in the report and perform required operations.

  • Zoom: This method lets you see the report in a magnified state. 

We can enable or disable this tool bar by setting the property "Display ToolBar" to True or False. We can also enable or disable each button on the toolbar by setting their individual properties to True or False in the property window. All of these properties can be set programmatically to make our report more dynamic.

We can enable or disable ToolBar programmatically as follows:


CrystalReportViewer1.Display ToolBar = False


We can set other properties in a similar fasion.

We can also use CrystalReportViewer to display data from other data sources such as XML files, flat text files, and so forth. With the help of Viewer you can even view multiple reports at once with tab reporting. You can refresh current data in the report. You can print a report as well as export a report in various formats such as .PDF, .RTF, Excel, Word and HTML.

blog comments powered by Disqus
.NET ARTICLES

- .Net 4.5 Brings Changes
- Understanding Events in VB.NET
- Objects, Properties, Events and Methods in V...
- Install Visual Web Developer Express 2010
- Microsoft Gadgeteer an Open Source Alternati...
- Best DotNetNuke Modules
- Facebook Image Viewer in Visual Basic
- Murach`s ADO.NET 4 Database Programming with...
- 5 Must Have Visual Studio 2010 Extensions
- Dynamic Web Applications with ASP.NET Mono u...
- PDFSharp: HTML to PDF in ASP.NET 3.5 using V...
- Using the PDFSharp Library in ASP.NET 3.5 wi...
- Sending Email in ASP.NET 3.5 using VB.NET wi...
- ASP.NET 3.5 Role Based Security and User Aut...
- Creating ASP.NET Login Web Pages and Basic C...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials