Printing a Report Without the Viewer Control
You can bypass the viewer control and print a report directly from a menu item or the click event of a button. Using the PrintToPrinter method of the ReportDocument class, you can print a loaded report directly to a printer. Listing 18.2 demonstrates how you could accomplish this in the click event of a button. Notice the code comments for the parameters for the PrintToPrinter method that sets the properties on the printed document.
Listing 18.2 Printing a Report with the PrintToPrinter Method
Private Sub button1_Click(sender As Object, e As System.EventArgs)
oRpt = New ReportDocument()
oRpt.Load("C:\Report1.rpt")
Try
oRpt.PrintOptions.PrinterName = "\jbnet\hp-color"
' The PrintToPrinter method parameters are:
' # of copies
' Collated
' Start Page
' End Page
oRpt.PrintToPrinter(1, True, 1, 1)
Catch err As Exception
MessageBox.Show(err.ToString())
End Try
End Sub
After you add this code to the click event of a button and modify the location of the printer and the report name, the file prints directly without a viewer control.
private void button1_Click(object sender, System.EventArgs e)
{
oRpt = new ReportDocument();
oRpt = new ReportDocument();
oRpt.Load(@"C:\Report1.rpt");
try
{
oRpt.PrintOptions.PrinterName =
"@\\jbnet\\hp-color";
// The PrintToPrinter method parameters are:
// # of copies
// Collated
// Start Page
// End Page
oRpt.PrintToPrinter(1, true, 1, 1);
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
}
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.