473,491 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Print dgv which have multiple number of pages

62 New Member
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
1 1368
rahul2310
62 New Member
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

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

Similar topics

6
4004
by: Wladimir Borsov | last post by:
I would like to load multiple web pages from Internet into ONE single browser window - one below the other. How do I do that most easily ? One idea is to built a frameset (for e.g 6 web pages)...
1
2013
by: mike | last post by:
Hello all, any advice or links to pages about how to have multiple language asp pages? For example, you have a database driven website that you want customers around the world to use. How do...
4
1858
by: EagleRed | last post by:
I am writing an ASP.NET 2.0 application that uses more than one master page. Currently, there are two pages, Freedom1.master and Freedom2.master. I have no problems with Freedom1.master. However,...
3
2776
by: Bart Van der Donck | last post by:
Hello, I'm having a hard time trying to configure printed output that consists of multiple pages. The idea is the following: <div style=" border: 1px solid blue; position: absolute; top:...
1
1610
by: LukeSkywacko | last post by:
Hi, I would like to know how I could accomplish this without using DB. There's a long text file named "TEST.TXT" The TEST file contains special character 'º' which will be used as pagebreak. I...
1
1345
by: Murty | last post by:
I am giving printout from windows xp. its give error message like "PRINTER CAN NOT PRINT SPOOLED REQUEST. SPOOLED PAGES ARE NOT PRINT "
0
1264
by: dan | last post by:
I have 5 aspx pages that I'd like to share the same code behind file. Those pages have the same set of 'core' controls and object data sources. Is this a good practice? It seems that VS2005...
1
1665
by: raul15791 | last post by:
Hi, I'm newbie in ASP. I am trying to view multiple pages by using a single file. For example, I have a few pages of product.asp (products.asp, products1.asp and so on). Now I want to create a...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.