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.

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

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

{mospagebreak title=Binding with the Report Source Property}

CrystalReportViewer is a class that belongs to the 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.

{mospagebreak title=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.

{mospagebreak title=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.

7 thoughts on “Using CrystalReportViewer to Display Crystal Reports in .Net

  1. Good article but I can’t find anywhere the information as to programmatically accessing those methods (the ones listed here in the article)???
    I.e. Export Report: This method is used to export the report
    Can you provide direction, or example? I’ve seen a few on other articles but Export, etc I can not find. Intellisense shows nothing! Thanx!

  2. we can export report programatically by using following code

    ReportName.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, “c:\FileName.pdf”)

    this code will export report ‘ReportName’ in pdf format at C:\ with document name as ‘FileName’

    Thank You,
    Madhuri Chanda

  3. I’m having problems when I try to load a Crystal Report that contains parameters. What I would like it to do is to prompt the user for the parameters, but I end up getting an exception, “Microsoft JScript runtime error: Sys.ArgumentException: Value must not be null for Controls and Behaviors.” In the debugger, if I ignore the error, eventually a page comes up that allows you to enter parameter values, and if I then enter a valid value, the Crystal Report is displayed correctly.

    What am I doing wrong? Is there a good way to programmatically ignore the exception? Any suggestions?

    CrystalDecisions.Web.Report rep;

    rep = new CrystalDecisions.Web.Report();
    String reportName;

    reportName = Request.QueryString[ “report” ];

    rep.FileName = reportName;

    CrystalReportSource1.Report = rep;
    CrystalReportViewer1.ReportSource = CrystalReportSource1;
    CrystalReportViewer1.Visible = true;
    catch( Exception ex )

  4. Hello Madhuri, Thanks for the very descriptive article. But, I am getting an error when I try to exit out of the application. ” COM object that has been separated from its underlying RCW cannot be used.” Can you please help me understand this error and resolve it. Thanks.

  5. HI,

    Its nice article guys and Thanks for sharing all information about CrystalReportViewer, I liked its all tools. Its very useful for all users.

  6. This is one of the best articles so far I have read online. Just useful information, Very well presented. Thanks for sharing with us. Check out this link too its also having nice post with wonderful explanation on CrystalReportViewer Control in ASP.Net and helped me lot to complete my task. For more details check out this link…..


[gp-comments width="770" linklove="off" ]