In 2005 Volume 1, a new control was added that allows the ability to Print Preview a PrintDocument. This WinPrintPreviewDialog™ uses two other controls that were also added in 2005 Volume 1, which are the WinPrintPreviewControl™ and WinPrintPreviewThumbnail™. The WinPrintPreviewDialog works with any .NET PrintDocument, this includes the WinGridPrintDocument™ that is used to print the WinGrid™ control. This topic focuses on using the WinGridPrintDocument with the WinPrintPreviewDialog.
Create a new Windows Application. With the form open in design view add the UltraGrid, and two Button controls to it. Then add an UltraPrintPreviewDialog, UltraGridPrintDocument, and the UltraDataSource.
Change both of the Buttons Text property so that one says "Print Preview" and the other says "Close".
Setup the UltraDataSource so it has one band and two columns. The screen shot below shows the two columns used. Add some random sample data into the UltraDataSource’s Data Entry area in its designer.
Go to the form’s code file, and create handlers for the Click events of the buttons, as well as for the Form Load event.
In the form’s Form Load event add the following code. The comments explain what each line is doing.
In Visual Basic:
Private Sub UsingtheWinPrintPreviewDialogwiththeWinGridPrintDocument_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load ' Set the Grid to use the UltraDataSource as its datasource Me.UltraGrid1.DataSource = Me.UltraDataSource1 ' Set the UltraGridPrintDocument's Grid property to the Grid to print Me.UltraGridPrintDocument1.Grid = Me.UltraGrid1 ' Set the UltraPrintPreviewDialog's Document property to the ' UltraGridPrintDocument Me.UltraPrintPreviewDialog1.Document = Me.UltraGridPrintDocument1 End Sub
In C#:
private void UsingtheWinPrintPreviewDialogwiththeWinGridPrintDocument_Load( object sender, System.EventArgs e) { // Set the Grid to use the UltraDataSource as its datasource this.ultraGrid1.DataSource = this.ultraDataSource1; // Set the UltraGridPrintDocument's Grid property to the Grid to print this.ultraGridPrintDocument1.Grid = this.ultraGrid1; // Set the UltraPrintPreviewDialog's Document property to the // UltraGridPrintDocument this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1; }
Next in the Click event for the button labeled Print Preview place the following code.
In Visual Basic:
Private Sub btnPrintPreview_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPrintPreview.Click Me.UltraPrintPreviewDialog1.ShowDialog(Me) End Sub
In C#:
private void btnPrintPreview_Click(object sender, System.EventArgs e) { this.ultraPrintPreviewDialog1.ShowDialog(this); }
In the Click event for the button labeled Close place the following code.
In Visual Basic:
Private Sub btnClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnClose.Click Me.Close() End Sub
In C#:
private void btnClose_Click(object sender, System.EventArgs e) { this.Close(); }
If you were to build and run your sample it would look similar to the screen shot below.
If you click on the Print Preview button you will see the WinGrid shown as it would, if you printed.
Related Topics