Hi,
I need to be able to print the entire contents of a datagridview or
even if you have to select/highlight the columns/rows to print.
I have the printdialog, printdocument and a printpreviewdialog on the
form already. I got some code from a friend to print but the code she
gave me was for printing the contents of a listbox and while I've
adjusted it a little, I know its not right.
I've been looking it up online a bit and found a good few sample
projects/code and all of them seem really complex - formatting the
contents of every cell, etc.
Here's the code I have so far anyway;
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.Rows
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
Just to add, what happens when I run the project is if I hit the print
button, the print dialog thing comes up, but I don't even have my
laptop connected to a printer, and if I bring up the print preview
thing, it has nothing to preview.
I know the line for each item as string in me.datagridview1.rows
probably isn't right is it?
Any advice on what I need to do to get this code going??
Thanks