Untyped with ReportDocument
Call the Load() method of the ReportDocument component. Pass it the fully qualified file path. Assign the ReportDocument object to the ReportSource property of the viewer.
Listing 2-2. Use the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
??? ByVal e As System.EventArgs) Handles MyBase.Load
??? reportDocument1.Load("C:\MyReport1.rpt")
??? CrystalReportViewer1.ReportSource = reportDocument1
End Sub
Listing 2-3. Print a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
??? ByVal e As System.EventArgs) Handles MenuPrint.Click
??? reportDocument1.Load("C:\MyReport1.rpt")
??? reportDocument1.PrintToPrinter(1, False, 0, 0)
End Sub
Strongly-Typed with ReportDocument
ReportDocument components generate all the necessary code for declaring and instantiating the report object for you behind the scenes. There is only one line of code you have to write. Assign the report object to the ReportSource property of the viewer.
Listing 2-4. Use the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
??????? ByVal e As System.EventArgs) Handles MyBase.Load
??? CrystalReportViewer1.ReportSource = reportDocument1
End Sub
Lising 2-5. Print a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
??????? ByVal e As System.EventArgs) Handles MenuPrint.Click
??? reportDocument1.PrintToPrinter(1, False, 0, 0)
End Sub
Untyped Using Code
If you are using the viewer, you have the option of setting the file path directly by using the Properties window or setting it in code. If you aren't using the viewer, you have to instantiate a report object and call its Load() method. If you set the PropertySource property in the Properties window, then no coding is required. The viewer will load the report automatically.
Lising 2-6. Uses the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
??????? ByVal e As System.EventArgs) Handles MyBase.Load
??? CrystalReportViewer1.ReportSource = "C:\MyReport1.rpt"
End Sub
Listing 2-7. Prints a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
??????? ByVal e As System.EventArgs) Handles MenuPrint.Click
??? Dim myReport As New ReportDocument()
??? myReport.Load("C:\MyReport1.rpt")
??? 'Set any printer specific properties here
??? myReport.PrintToPrinter(1, False, 0, 0)
End Sub
Strongly-Typed Using Code
The code to instantiate the object and print or preview it is your responsibility. Create an object variable from the report class and instantiate it.
Listing 2-8. Use the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
??????? ByVal e As System.EventArgs) Handles MyBase.Load
??? Dim myReport As New CrystalReport1()
??? 'Set any printer specific properties here
??? CrystalReportViewer1.ReportSource = myReport
End Sub
Listing 2-9. Print a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
??????? ByVal e As System.EventArgs) Handles MenuPrint.Click
??? Dim myReport As New CrystalReport1()
??? 'Set any printer specific properties here
??? myReport.PrintToPrinter(1, False, 0, 0)
End Sub
|