Connecting Tech Pros Worldwide Help | Site Map

Printing the entire contents of a datagridview

  #1  
Old February 18th, 2009, 01:14 PM
Newbie
 
Join Date: Feb 2009
Posts: 9
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
  2.         If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  3.             Me.PrintDocument1.Print()
  4.         End If
  5.     End Sub
  6.  
  7.     Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  8.         Dim x As Integer = e.MarginBounds.Left
  9.         Dim y As Single = e.MarginBounds.Top
  10.         Dim lineheight As Single = Me.DataGridView1.Font.GetHeight(e.Graphics)
  11.         For Each item As String In Me.DataGridView1.Controls
  12.             e.Graphics.DrawString(item, Me.DataGridView1.Font, Brushes.Black, x, y)
  13.             y += lineheight
  14.         Next
  15.     End Sub
  16.  
  17.     Private Sub PrintPreviewToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem1.Click
  18.         PrintPreviewDialog1.ShowDialog()
  19.     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
  #2  
Old May 10th, 2009, 01:36 AM
Newbie
 
Join Date: May 2009
Posts: 1

re: Printing the entire contents of a datagridview


Did you ever get this working. I have to do the same thing and can't get it to work. Any help would be appreciated.
  #3  
Old July 2nd, 2009, 12:29 PM
Newbie
 
Join Date: Jun 2009
Posts: 1

re: Printing the entire contents of a datagridview


i have one example like what you wanted it .. but i cant upload it here .. and i cant post a new topic .. :(
Reply