By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

How to Print dgv which have multiple number of pages

P: 62
I am new to vb.net windows form
I have code (bit map method) using which i can print data grid but only one page not if grid has multiple pages(many rows)
i want to print all the rows around 90 +
Please help me with code to print dgv

Code i am using now is below
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprint.Click
  2.         PrintPreviewDialog1.Document = PrintDocument1
  3.         PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
  4.         PrintPreviewDialog1.ShowDialog()
  5.     End Sub
  6.  
  7.  
  8.     Private Sub PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  9.         Dim bm As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
  10.         DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
  11.         e.Graphics.DrawImage(bm, 0, 0)
  12.     End Sub
  13.  
Mar 22 '14 #1
Share this Question
Share on Google+
1 Reply


P: 62
i found it by googling around if some one gets stucked hole my answer helps,do following
•First add Print Document1 and Print Preview Dialog1 to form on which data grid is placed from tool box.
•Then select print document1, go to properties and go to print page event
•In print page event write following code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. With DataGridView1
  3. Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
  4. fmt.LineAlignment = StringAlignment.Center
  5. fmt.Trimming = StringTrimming.EllipsisCharacter
  6. Dim y As Single = e.MarginBounds.Top
  7. Do While mRow < .RowCount
  8. Dim row As DataGridViewRow = .Rows(mRow)
  9. Dim x As Single = e.MarginBounds.Left
  10. Dim h As Single = 0
  11. For Each cell As DataGridViewCell In row.Cells
  12. Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
  13. e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
  14. If (newpage) Then
  15. e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
  16. Else
  17. e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
  18. End If
  19. x += rc.Width
  20. h = Math.Max(h, rc.Height)
  21. Next
  22. newpage = False
  23. y += h
  24. mRow += 1
  25. If y + h > e.MarginBounds.Bottom Then
  26. e.HasMorePages = True
  27. mRow -= 1
  28. newpage = True
  29. Exit Sub
  30. End If
  31. Loop
  32. mRow = 0
  33. End With
  34.  
  35.  
  36.  
•On Click of print button write following code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. PrintPreviewDialog1.Document = print
  3. PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
  4. PrintPreviewDialog1.ShowDialog()
  5.  
  6.  
  7.  
Bye hope it helps
Aug 28 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.