Here's code to print, printpreview, & pagesetup for a datagrid
Here is the code for all of those that would like to enable printPreview, PageSetup, and Print on a datagrid:
Imports System.Drawing.Printing
Private PrintPageSettings As New PageSettings()
'For PrintPreview
Private Sub PrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreview.Click
Try
PrintDocument1.DefaultPageSettings = PrintPageSettings
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'For PageSetup
Private Sub PageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetup.Click
Try
PageSetupDialog1.PageSettings = PrintPageSettings
PageSetupDialog1.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'To Print
Try
PrintDocument1.DefaultPageSettings = PrintPageSettings
PrintDialog1.Document = PrintDocument1
Dim Result As DialogResult = PrintDialog1.ShowDialog
If Result = DialogResult.OK Then
PrintDocument1.Print()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Also put this code in the PrintDocument _PrintPage Event:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim myArgs As New PaintEventArgs(e.Graphics, New Rectangle(New Point(0, 0), Me.Size))
Me.InvokePaint(DataGrid1, myArgs)
End Sub
Only one problem still remains : PRINTING MULTIPLE PAGES!