The integration of Crystal Reports in Visual Studio .NET and availability of wizards and tools has made writing reports a piece of cake for developers. Visual Studio .NET hides all the details from developers and provides them easy to use user interface so they can concentrate on designing the layouts of the reports. But it's always a good idea to understand what goes under the hood. In this article, I will explore the Crystal Report Viewers and their functionality.
Crystal Report Viewers work as a container of a report and allow us to view a crystal report in Windows and Web applications. Crystal Reports for Visual Studio .NET provides two report viewers - Windows Forms Viewer and Web Forms Viewer. As you can guess from these names, Windows Forms Viewer control is used to view reports in Windows Forms applications and Web Forms Viewer is used to display reports in Web Forms applications.
Crystal Reports Viewer for Windows Forms
The CrystalReportViewer class defined in the CrystalDecisions.Windows.Forms namespace represents a Crystal Report Viewer for Windows Forms (viewer afterwards). This class is a user control and can be used as any other user controls. This control is available in the Toolbox and you can simply drag it to a Form to add the control to the form.
To view a report in the viewer, you simply need to set the ReportSource property of the control. The ReportSource property can be either a full name of the report file, a strongly typed report, or a ReportDocument object.
The following code snippet binds a report with hard coded full path of the report to the viewer:
crystalReportViewer1.ReportSource = @"C:\temp\Customers.rpt";
The following code snippet binds a strongly typed report to the viewer:
Customers custReport = new Customers();
crystalReportViewer1.ReportSource = custReport;
The following code snippet creates a ReportDocument object, loads a report using the Load method and binds the ReportDocument object to the viewer:
CrystalDecisions.CrystalReports.Engine.ReportDocument doc =
new CrystalDecisions.CrystalReports.Engine.ReportDocument();
doc.Load(@"C:\temp\Customers.rpt");
crystalReportViewer1.ReportSource = doc;
See my article Introduction to Crystal Reports in .NET to understand how to add a Crystal Report Viewer to a Windows Forms application and how to generate a strongly typed report.
Customizing the Behavior of Crystal Report Viewer
Using the CrystalReportViewer class members, we can customize the behavior of the viewer such as disabling or enabling viewer toolbar buttons, adding custom zoom factor, or moving to different views of the report programmatically. The default Crystal Report Viewer looks like the following:

The first four buttons on the viewer allow us to move first, previous, next, and last page of the report. The next button can be used to jump to a particular page. Next three buttons are used to close the view, print the report, and refresh the report respectively.
Next button allows you to export reports as to various files such as a pdf, tif, doc, or rtf. The next button is a toggle for grouping if a report has groups. Next two buttons are for zooming and search.
By using the properties and methods of CrystalReportViewer , we can customize the default settings of the viewer. Some of the common properties of CrystalReportViewer are following:
ActiveViewIndex and ViewCount
A page in the report is called a view. The active page is called active view. The ActiveViewIndex property gets or sets the active view in the viewer and ViewCount returns total number of views in a report.
DisplayToolbar
You can display or hide the toolbar using this property. The following code snippet hides the toolbar.
crystalReportViewer1.DisplayToolBar = false;
ShowCloseButton, ShowExportButton, ShowGotoPageButton, ShowGroupTreeButton, ShowPrintButton, ShowRefreshButton, ShowTextSearchButton, and ShowZoomButton properties can be used to show or hide the respective buttons.
CrystalReportViewer Methods
We can manage the functionality of the viewer buttons programmatically using its members. The CrystalReportViewer class provides methods to export, print, refresh, close, search, zoom, and navigation functionality. The following table lists the CrystalReportViewer class methods:
| CloseView | Closes a view tab in the viewer. |
| DrillDownOnGroup | Drills down on a group. |
| ExportReport | Exports the report displayed in the viewer. |
| GetCurrentPageNumber | Gets the current page number of the report. |
| PrintReport | Prints the report displayed in the viewer. |
| RefreshReport | Refreshes the report displayed in the viewer. |
| SearchForText | Searches the report for the given text. |
| ShowFirstPage | Shows the first page of the report. |
| ShowGroupTree | Shows the group tree in the viewer. |
| ShowLastPage | Shows the last page of the report. |
| ShowNextPage | Shows the next page of the report. |
| ShowNthPage | Shows the specified page of the report. |
| ShowPreviousPage | Shows the previous page of the report. |
| Zoom | Changes the magnification level of the viewer |
I will cover Crystal Report Viewer for Web Forms in my forthcoming articles.

adi rahmadPosted Apr 8, 2014, 2:11 AM
Hello brother I want to make printersetup at crystal report viewer in VB.Net for change size the paper, can you help me..? thanks
Nhat TongPosted May 2, 2012, 5:44 AM
Hi, I have a question about crystal report in visual studio 2008. I use object crystal report viewer for show my report: crystalReportViewer.ReportSource = reportDocument; crystalReportViewer.ParameterFieldInfo = report.ParameterFields; crystalReportViewer.Refresh(); When I change parameter of report on user interface , memory of program is increase about 3-5 MB. How can I clean old data before change parameter ? I don't know where is data of report. Thanks so much!
Neeraj SinghalPosted Nov 23, 2011, 1:00 PM
Hai Mahesh! I want to show data in crystal report using checkbox with respective table columns. when I run my project and want to show crystal report only respective column name is shown in crystal report but respective data is not show. plz help me regarding this. Thanks.!
Juan CuellarPosted Oct 3, 2011, 5:41 PM
hello, I have a question, I keep getting an error on crystal reports. Error load report failed - invalid report file path? How can i fix this error
Armando Gayoso JrPosted Aug 14, 2011, 11:28 PM
Hi Mahesh, I have a web application (asp.net) that generate report using Crystalreportviewer. I have a viewreport button for load event. It successfully displayed the first page of the report but after clicking on the next button of the crystal report toolbar, it send me an error "Load report failed". Hope you can help on this. Thank you.
Ron FaulknereditedPosted May 9, 2011, 2:21 PMEdited May 9, 2011, 2:24 PM
Hi Mahesh. Is it possible to display the toolbar items in the CrystalReportViewer application in French (i.e. ZoomFactor in french)?
d dPosted Mar 8, 2011, 7:03 PM
How do I get my report to open in pdf by default rather than exporting to pdf from the viewer?
Aldrin VerdijoPosted Jan 6, 2011, 12:22 AM
Please help me how can i access the closeview button in crystalreportviewer. It has been disabled everytime the data has been viewed.. I want that everytime i search data to view in reportviewer, I want to the crystalreporviewer to close the opened report and back to nothing.. please help me.
DhanasekaranPosted Aug 19, 2010, 8:18 AM
Hi chand: How to display the total records and affected records in the tool bar ? ex : 60 / 5000 60 records has been selected out of 5000 records.
marjaneh khPosted Apr 18, 2010, 3:57 AM
hi I want to have a report in my web site, but i can't bind crystalreportviewer, i wrote this code: SqlConnection connection = new SqlConnection ("server=localhost;initial catalog=viewofplists;user id=test;password=123456"); SqlDataAdapter DataAdapter = new SqlDataAdapter("select family,title,facultyfamily,studentno from Query",connection); DataSet Mydataset = new DataSet(); DataAdapter.Fill(Mydataset,"Query"); CrystalReport CrystalReport = new CrystalReport(); CrystalReport.SetDataSource(Mydataset); CrystalReportViewer1.ReportSource = CrystalReport; but debugger said in this line U miss a using directory or reference: CrystalReport CrystalReport = new CrystalReport(); please write for me complete code.
Divik GargPosted Dec 4, 2009, 12:32 AM
Dear Mahesh, Clicking on the group tree is creating the following exception. "Get page number for group failed. " !!!
Muhammad TalalPosted Oct 27, 2009, 8:29 PM
actually i have generated a crystal a reoprt and and i want view it on run time in windows form aplication
Muhammad TalalPosted Oct 27, 2009, 8:27 PM
i am geting this eror when i m using crystal report viewer this is the actual code which i am using ReportDocument doc = new ReportDocument(); doc.Load(@"F:\talal folder\project\Dgwaiter(FINAL)\WindowsApplication2\WindowsApplication2\CrystalReport1.rpt"); CrystalReportViewer1.ReportSource = doc;
Trendra ChauhanPosted Sep 16, 2009, 2:15 AM
Hi Mahesh, my report showing fine but when i click on next button on report viewer toolbar Error Load report failed found. Please HELP me.
Rhoziele SwingPosted Jun 17, 2009, 8:27 PM
Hi Mahesh, Please help me. Im creating a crystal reports and it works fine. But the problem is, when I click the next button, it only go to 2nd page, then it does not go to the remaining page. Here's my code: Protected Sub ButtonNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonNext.Click me.LORcrViewer.ReportSource = LORReports Me.LORcrViewer.ShowNextPage() End Sub
Niyaz AhmadPosted May 26, 2009, 5:38 AM
How we could place the icon on the different places on the crystal reports through VB.Net 2003 dynamically. Please provide a solution of this particular problem because am facing a lot. Thanks Niyaz Ahmad Email: [email protected] [email protected]
brianPosted Apr 27, 2009, 6:02 PM
Can anyone provide input on how I may be able to add a new button (to provide custom functionality) to the CR toolbar? I know crystalReportViewer exists, which makes it easy to disable the existing buttons, but I am having difficulty figuring out how to add a new button. Any help appreciated! Thanks...
Jessica ChenPosted Apr 20, 2009, 4:18 PM
Hi Mahesh, I have a trial version of crystal report 11. I am trying to find an export to Microsoft word(doc) option from its Export format drop down list but can't find one. I only have Microsoft word(rtf) and Microsoft word editable(rtf). Since I find there are many methods of the Reports Viewer Control missing in the trial version, such as "ExportReport", I am wondering if the normal version has this option for (*.doc) or not. Thanks a lot. Jessica
syampavanPosted Apr 7, 2009, 3:13 AM
Hi mahesh, i have developed a asp.net(2.0) web application which has several crystal reports in it. Everything works fine in my local system. But the problem arise when i deploy my website on the iis server in a window server 2003 machine. Can you please describe step by step procedure for deploying a .net webapplication with crystal reports on iis. Please mention even the minute things in detail(Eg- how and from where to get the msi required).
syampavanPosted Apr 7, 2009, 3:11 AM
Hi mahesh,
topaz softPosted Jan 22, 2009, 1:33 AM
Hello sir I want to ask about creating crystal report in asp.net. We create the crystal report using Data Source Name(DSN). We create data table and pass it according to user selection. Only the problem in crystal report viewer when loaded records in data table has been repeated in .rpt file. Please provide suggestion regards this..
PaulPosted Nov 6, 2008, 5:14 AM
Hi Mahesh, I created a report using crystal reports X1. I then created an aspx page which just calls the report. But when I view the report on the web page, I am not able to edit the group tree of the crystal reports that appears on the left hand side. and hence not able to view the group name. Is there any way by which I can correct this problem? Thanks, Paul
wtPosted Oct 19, 2008, 11:28 PM
hi, i have a windows application.. is there any way to close only the report in the report viewer? can u help me please.. :) thanks so much! -joyce
Mic keeeyPosted Nov 12, 2007, 5:55 AM
Hi, Could you help me? I need to view reports that have more than one page. The problem is, when I select the "Next" button on the CrystalReportViewer control, an error appears stating "Access Denied". Underneath that message is another message stating.... "An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below." and...then the stack trace bit appears below that. Could you help me get it so that when I click the "Next" and "Previous" button on the CrystalReportViewer control that I am able to view these pages without an error? Would be most grateful. Thank you in advance...
kevin wuPosted Nov 7, 2007, 4:36 AM
hi Mahesh i am a new guy in crystal report i try to desgin a crystal report use the vs2005 and a got the data but the forms didn't show the grouptree how can i do (i try CrystalReportViewer1.ShowGroupTree() and set displayGroupTree=true) but also can't show the grouptree please give me a hand thanks kevin
QuinnPosted Jul 26, 2007, 2:09 AM
I'm using VS .Net 2005 built-in crystal report to create a report in C#. Using stored procedure as the data source. Setting the parameters using SetParameterValue(). Parameters are being set well. But when i run the report the message 'Parameter was not supplied' pops up. Can u please help on this? Thanks
QuinnPosted Jul 26, 2007, 1:51 AM
I'm using VS .Net 2005 built-in crystal report to create a report in C#. Using stored procedure as the data source. Setting the parameters using SetParameterValue(). Parameters are being set well. But when i run the report the message 'Parameter was not supplied' pops up. Can u please help on this? Thanks
TheodorePosted Jun 7, 2007, 2:27 AM
Hi Mahesh, I'm new to Visual Studio 2005 and using the VS2005_Walkthroughs to help me learn through examples. So, I opened the above example available in TutorialSampleCodesProjects through VS 2005_Walkthroughs hit CTRL-F5 and it worked, however when I entered some criteria and then clicked the 'redisplay report' button it did not refresh the report with the entered criteria. It retrieved all data. It's like the selectionformula is not being 'activated' ??? I am using Win XP Pro and I think IIS is configured correctly. Below is the code directly out of the example (Default.aspx.vb). This is giving me all sort of grief. Thanking you in advance Mahesh. Theodore Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Partial Class _Default Inherits System.Web.UI.Page Private Sub ConfigureCrystalReports() selectOperatorList.DataSource = System.Enum.GetValues(GetType(CeComparisonOperator)) selectOperatorList.DataBind() selectOperatorList.DataSource = System.Enum.GetValues(GetType(CeComparisonOperator)) If Not IsPostBack Then selectOperatorList.DataSource = System.Enum.GetValues(GetType(CeComparisonOperator)) Dim mySelectFormula As String = "{Customer.Last Year's Sales} > 11000.00 " _ & "AND Mid({Customer.Customer Name}, 1, 1) = ""A"" " myCrystalReportViewer.SelectionFormula = mySelectFormula selectOperatorList.DataBind() End If Dim reportPath As String = Server.MapPath("CustomersBySalesName.rpt") myCrystalReportViewer.ReportSource = reportPath End Sub Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init ConfigureCrystalReports() End Sub Protected Sub redisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles redisplay.Click Dim mySelectedOperator As String = GetSelectedCompareOperator() Dim mySelectFormula As String = "{Customer.Last Year's Sales} > " & lastYearsSales.Text _ & " AND Mid({Customer.Customer Name}, 1) " & mySelectedOperator & " """ & customerName.Text & """" myCrystalReportViewer.SelectionFormula = mySelectFormula myCrystalReportViewer.ReportSource = Server.MapPath("CustomersBySalesName.rpt") End Sub Private Function GetSelectedCompareOperator() As String Select Case selectOperatorList.SelectedIndex Case CeComparisonOperator.EqualTo Return "=" Case CeComparisonOperator.LessThan Return "<" Case CeComparisonOperator.GreaterThan Return ">" Case CeComparisonOperator.LessThan_or_EqualTo Return "<=" Case CeComparisonOperator.GreaterThan_or_EqualTo Return ">=" Case CeComparisonOperator.Not_EqualTo Return "<>" Case Else Return "=" End Select End Function End Class
son vuongPosted May 29, 2007, 4:50 AM
i'm open a ReportDocument so set ReportSource of ReportViewer then i close ReportDocument that is export not execute
mikeePosted Mar 16, 2007, 12:54 PM
Hi Do you know how i can stop the Reportprocess? Some Reports with many Data goes 2-3minutes. In earlier Crystal Report Version was a Button to stop Reports with many Data
JoePosted Mar 7, 2007, 9:22 PM
Hi, i'm using vs.net 2003 (vb.net). is it possible for the crystal report, when click on print button, it automatically print the report without showing the printer dialog? if it is possible, pls help me.. how to do it? thx.
JoePosted Mar 7, 2007, 9:17 PM
Hi, i'm using vs.net 2003 (vb.net). is it possible for the crystal report, when click on print button, it automatically print the report without showing the printer dialog? if it is possible, pls help me.. how to do it? thx.
dollina mendoncaPosted Feb 2, 2007, 1:04 AM
Hello Sir I have designed report outside application in Seagate crystal report 7.0 and attached report to C# application using Report viewer. Plz tell me how to pass values to formulas created in crystal report from C# application ,an my database is having passward.
BrianPosted Jan 23, 2007, 1:34 PM
I have a Windows app and am using Crystal XI. I cannot find the correct way to close a report in the report viewer without disposing of the report viewer control itself. ??
SandeepPosted Jan 2, 2007, 6:24 AM
Hi I have migrate my asp.net application to 2005 from 2003, but still i am using crystal report assemblies of version 9. I have a report web page where i load my report files dynamically. This page is working fine but the Zoom control is not working i am not able to understand why? thank you.. sandeep...
Mahesh ChandPosted Dec 11, 2006, 3:06 PM
This example demonstrates how to use to set some of the properties of the MicrosoftMailDestinationOptions class. [Visual Basic] Public Sub ExportToMSMail(ByVal toList As String, ByVal subject As String, ByVal message As String, _ ByVal ccList As String, ByVal user As String, ByVal password As String) ' Declare variables and retrieve the export options. Dim pdfOpts As PdfRtfWordFormatOptions = _ ExportOptions.CreatePdfRtfWordFormatOptions() Dim mailOpts As MicrosoftMailDestinationOptions = _ ExportOptions.CreateMicrosoftMailDestinationOptions() Dim exportOpts As ExportOptions = New ExportOptions ' Set the PDF format options. pdfOpts.UsePageRange = False exportOpts.ExportFormatOptions = pdfOpts ' Set the mail options. mailOpts.MailCCList = ccList mailOpts.MailMessage = message mailOpts.MailSubject = subject mailOpts.MailToList = toList mailOpts.Password = password mailOpts.UserName = user exportOpts.ExportDestinationOptions = mailOpts ' Export the report. exportOpts.ExportDestinationType = _ ExportDestinationType.MicrosoftMail exportOpts.ExportFormatType = _ ExportFormatType.PortableDocFormat Report.Export(exportOpts) End Sub
Mahesh ChandPosted Dec 11, 2006, 3:06 PM
This example demonstrates how to use to set some of the properties of the MicrosoftMailDestinationOptions class. [Visual Basic] Public Sub ExportToMSMail(ByVal toList As String, ByVal subject As String, ByVal message As String, _ ByVal ccList As String, ByVal user As String, ByVal password As String) ' Declare variables and retrieve the export options. Dim pdfOpts As PdfRtfWordFormatOptions = _ ExportOptions.CreatePdfRtfWordFormatOptions() Dim mailOpts As MicrosoftMailDestinationOptions = _ ExportOptions.CreateMicrosoftMailDestinationOptions() Dim exportOpts As ExportOptions = New ExportOptions ' Set the PDF format options. pdfOpts.UsePageRange = False exportOpts.ExportFormatOptions = pdfOpts ' Set the mail options. mailOpts.MailCCList = ccList mailOpts.MailMessage = message mailOpts.MailSubject = subject mailOpts.MailToList = toList mailOpts.Password = password mailOpts.UserName = user exportOpts.ExportDestinationOptions = mailOpts ' Export the report. exportOpts.ExportDestinationType = _ ExportDestinationType.MicrosoftMail exportOpts.ExportFormatType = _ ExportFormatType.PortableDocFormat Report.Export(exportOpts) End Sub
CarriePosted Dec 7, 2006, 8:47 PM
I am using Visual Studion 2005 specifically VB.net. I am using crystal reports and launching them to the crystal viewer supplied with visual studio. This is working great. How do I export to email? All the other export capapbilities I can find, but I haven't been able to find the answer to this question. Any suggestions? Carrie
Prashant ChoudharyPosted Dec 2, 2006, 1:28 AM
Hi Friends I have 2 problem in Crystal Reports. 1] Can i trap the goto event button in .Net because supose my reports containg 5 pages , now i am on 1 st page and pressing goto button and then enter the page number 15 then viewer show me the last page but i want to put a message box that reports contains only 5 pages. 2]In My report viewer "Total page No :1+ " why its not showing the exact page number like "Total page No :5"
nu aungPosted Nov 10, 2006, 1:55 AM
Dear Mr.Mahesh, I would like to know the print button click event of CrystalReportViewer1. I have to change status in some user table under that button click event. My software need to change status in some user table after printing report. I use vb.net 2003. Thanks, Nu.
kavitaPosted Oct 18, 2006, 2:55 AM
hi mahesh I m kavita,A S/W developer in Delhi. Currently I m Working On A ERP Project(C# With ASP.NET). My Problem Is I don't want to use toolbar in crystal report because it display when I run My Web Application & Also Print with my report. Is there any way to short out this problem if I m using the Nextpage property then (suppose my report filter 3 pages from 10 total pages) the report show all the 10 pages. please help me. also is it possible if I click a record in crystal report it goes to another webForm or Report
smilePosted Jul 31, 2006, 10:19 PM
Hi Mahesh, I am using VS.NET 2002 for windows application. I used the Crystal Report Viewer and enabled its print button. When click on print button, I realised that the "number of copies" does not work at all. It always prints out 1 copy. Is there a way I can override this setting in coding? Pls advise. Thanks.
Sudipto PauleditedPosted Jul 24, 2006, 4:25 AMEdited Nov 7, 2006, 12:24 PM
Hello Mahesh, It may be very strange but my application is giving this error for some time on some machines and working fine on some other.I'm using VS 2002 with crystal reports. When i debug on the machine the error was thrown while initializing the report viewer.
lakshmi prasadPosted Jul 18, 2006, 2:56 AM
i need clear description about mobile technology in .net .and the need of sotwares for mobiletechnology.. thanking u ur Well wisher D.Prasad JSoftware Factory
Jo sreePosted Mar 6, 2006, 2:48 AM
Hai Mahesh! I have to develop crystal reports in ASP.NET.I have generated a report using the wizard, and given it as the datasource to the crystalreportViewer, the data is loaded on to the page(design time) but i can view the page when i run it it gives an error as LOGIN Failed. Can u plz help me Thanks josree