Hi,
I need to be able to print the contents of a datagrid view. I have the printdialog, printdocument and printpreview dialog controls put on my form.. Here is my code so far:
- Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
-
If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
-
Me.PrintDocument1.Print()
-
End If
-
End Sub
-
-
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
-
Dim x As Integer = e.MarginBounds.Left
-
Dim y As Single = e.MarginBounds.Top
-
Dim lineheight As Single = Me.DataGridView1.Font.GetHeight(e.Graphics)
-
For Each item As String In Me.DataGridView1.Controls
-
e.Graphics.DrawString(item, Me.DataGridView1.Font, Brushes.Black, x, y)
-
y += lineheight
-
Next
-
End Sub
-
-
Private Sub PrintPreviewToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem1.Click
-
PrintPreviewDialog1.ShowDialog()
-
End Sub
I know the code under printdocument_printpage isn't right. But how do I get everything in the datagridview selected to send it to print? I don't want the user to have to go highlighting the datagridview.
Your help would be really appreciated.
Thanks