Binding Reports to the Viewer
The viewer needs to be told which report to display. This is called binding the report to the viewer. The property used for binding is the ReportSource property. There are three ways to set the ReportSource property: using the ReportDocument component, passing it an Untyped report filename, and passing it a Strongly-Type report class name. Each method is covered in the next three sections.
Using the ReportDocument Component with the Viewer
Adding a ReportDocument component to the form is the easiest way to specify which report to print. Everything about it is visual and there is no code to write. Add it to your form by going to the Components section of the Toolbox and double-clicking on it. It will automatically display the Choose a ReportDocument dialog box. This lets you select which report to display. The dropdown control lists all the reports that are part of your project. Once you select which report to use, the dialog box closes and it the component gets added to your form.
The ReportDocument component has to be on the same form as the viewer control. The viewer can’t reference a ReportDocument component on another form.
After the component is added to the form, tell the viewer to use it by clicking on the viewer’s ReportSource property and selecting the ReportDocument’s name from the dropdown list.
When you run the application and open the form, the viewer automatically displays the report to the user. You don’t have to write any code.
Using Untyped Reports with the Viewer
Untyped reports are, by definition, referenced by their filename. The viewer loads the report into memory and displays it to the user. The viewer’s ReportSource property has to be passed the report’s filename.
Setting the ReportSource property can be done anywhere within your form. For example, you could call it in response to the user clicking on a button or a menu item. In this example (and almost all the examples in this book), I put the code in the form’s Load() event. I do this because it is easy to understand and it makes the form preview the report immediately. In this example the report used is the Employee List report created in Chapter 1. You should replace the report filename with your own report.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ReportSource = "Employee List.rpt"
End Sub
Using Stongly-Typed Reports with the Viewer
Strongly-Typed reports are referenced by their class name. Create a new instance of the report class and pass it to the viewer’s ReportSource property. As mentioned in the last section, the code in this example is called within the form’s Load() event so that the report is displayed immediately upon opening the form. The Employee_List class name is from the example report in Chapter 1. Replace it with the class name of the report in your own application.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ReportSource = New Employee_List
End Sub
Notice that the report’s class name is “Employee_List” and the actual report name is “Employee List”. When there are spaces in the report name, the class name that .NET creates will use underscores instead of spaces.