473,513 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Print datagridview How to...

Hi,

Hope someone can help me
I have a datagridview I want to print

Code below works fine if I print the data if there is only 1 page (without
using hasmore pages
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If intRowPos <= 10 Then
e.HasMorePages = False
Else
e.HasMorePages = True
End If
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)

If I include "If e.HasMorePages... " as below everything goes wrong
when I need to print more pages
the hole print goes on one page and continue copy the first many times!!

-------------------------------------------------------------------------------------------------------

Many thanks in advance

'***Code Print datagridview***

Imports System.Data.OleDb
Imports System.Data
Imports System.Web
Imports System.Globalization

Public Class PrintDGV
Dim WithEvents PrtDoc As New Drawing.Printing.PrintDocument
Private intRowPos As Int16

Private Sub btnPrintInvoice_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrintInvoice.Click

Dim PreviewDialog As New PrintPreviewDialog()
PreviewDialog.Document = PrtDoc
PreviewDialog.WindowState = FormWindowState.Maximized
PreviewDialog.PrintPreviewControl.Zoom = 1.0
PreviewDialog.ShowDialog()

End Sub

Private Sub Prtdoc_PrintPage(ByVal sender As System.Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles PrtDoc.PrintPage

Dim IntLijnteller As Integer = 1
Dim Borstel As New Drawing.SolidBrush(Color.Black)
Dim MyFont As New Font("Bookman Old Style", 12, FontStyle.Regular)
Dim TekstAfmetingen As New SizeF
Dim IntPrintHo As Integer

Static oColumnLefts As New ArrayList
Static oColumnWidths As New ArrayList
Static oColumnTypes As New ArrayList
Static iX As Integer = 0

Dim nTop As Int16 = CShort(e.MarginBounds.Top)
Dim nLeft As Int16 = CShort(e.MarginBounds.Left)
Dim strRef3 As String = ""
Dim strArtikel4 As String = ""
Dim intAantal5 As Integer = 0
Dim dPrijs7 As Decimal
Dim dKorting8 As Decimal
Dim dTotaal10 As Decimal

IntPrintHo = 100
Dim rowNumber As Integer = 1
Dim row As DataGridViewRow

e.Graphics.PageUnit = GraphicsUnit.Millimeter
MyFont = New Font("Bookman Old Style", 12, FontStyle.Regular)

Dim potlood As New Drawing.Pen(Color.DarkBlue)
potlood.Width = 0.1

IntLijnteller = 1
IntPrintHo = 100

For Each row In dgvFacturenInfo.Rows
strRef3 = String.Empty
strArtikel4 = String.Empty
intAantal5 = CInt(Nothing)

'Printing Reference
strRef3 = CStr(dgvFacturenInfo.Rows(row.Index).Cells(2).Valu e)
e.Graphics.DrawString(strRef3, MyFont, Borstel, 15, IntPrintHo)

'Printing text
strArtikel4 = CStr(dgvFacturenInfo.Rows(row.Index).Cells(3).Valu e)
e.Graphics.DrawString(strArtikel4, MyFont, Borstel, 40, IntPrintHo)

Dim recf5 As New RectangleF(10, IntPrintHo, 133, IntPrintHo)
Dim hh5 As New StringFormat
hh5.Alignment = StringAlignment.Far

'Printing Quantity
intAantal5 = CInt(CStr(dgvFacturenInfo.Rows(row.Index).Cells(4) .Value))
e.Graphics.DrawString(CStr(intAantal5), MyFont, Borstel, recf5, hh5)

'Printing Price
dPrijs7 = CType(((dgvFacturenInfo.Rows(row.Index).Cells(9).V alue)), Decimal)
Dim recf7 As New RectangleF(10, IntPrintHo, 155, IntPrintHo)
Dim hh7 As New StringFormat
hh7.Alignment = StringAlignment.Far
dPrijs7 = CType(((dgvFacturenInfo.Rows(row.Index).Cells(9).V alue)), Decimal)
e.Graphics.DrawString(CStr(CDec(Format(dPrijs7, "#,###0.00"))), MyFont,
Borstel, recf7, hh7)

'Printing Profit
dKorting8 = CType(((dgvFacturenInfo.Rows(row.Index).Cells(10). Value)),
Decimal)
e.Graphics.DrawString(CStr(Format(dKorting8, "0.00")), MyFont, Borstel, 170,
IntPrintHo)

'Printing Totals
Dim recf10 As New RectangleF(10, IntPrintHo, 190, IntPrintHo)
Dim hh10 As New StringFormat
hh10.Alignment = StringAlignment.Far
dTotaal10 = CDec(((dgvFacturenInfo.Rows(row.Index).Cells(13).V alue)))
e.Graphics.DrawString(Format(dTotaal10, "#,###0.00").ToString, MyFont,
Borstel, recf10, hh10)
IntPrintHo = IntPrintHo + 5
intRowPos = intRowPos + 1

'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! This makes the wrong outprint
If intRowPos <= 10 Then
e.HasMorePages = False
Else
e.HasMorePages = True
End If
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Next

End Sub

End Class

Mar 26 '07 #1
2 2411
>>For Each row In dgvFacturenInfo.Rows

I may be wrong here but it looks like to me the above line starts
printing every new page at the beginning of the DataGridView as it
starts looping with the first row in the grid.

Instead of that loop, you need to use something like:

For RowIndex = WhereTheLastPrintPageEnded To
WhereTheLastPrintPageEnded + NumberOfRowsThatFitOnAPage
row = dgvFacturenInfo.Rows(RowIndex)

At the bottom of the code, you would need to make sure
WhereTheLastPrintPageEnded is set properly and it would have to be a
form level field so it woould retain its values between calls.

====================
Clay Burch
Syncfusion, Inc.

Mar 26 '07 #2
Hi ClayB,

Many thanks for answering, I will try your solution

Marc.

"ClayB" <cl***@syncfusion.comschreef in bericht
news:11*********************@p77g2000hsh.googlegro ups.com...
>>>For Each row In dgvFacturenInfo.Rows

I may be wrong here but it looks like to me the above line starts
printing every new page at the beginning of the DataGridView as it
starts looping with the first row in the grid.

Instead of that loop, you need to use something like:

For RowIndex = WhereTheLastPrintPageEnded To
WhereTheLastPrintPageEnded + NumberOfRowsThatFitOnAPage
row = dgvFacturenInfo.Rows(RowIndex)

At the bottom of the code, you would need to make sure
WhereTheLastPrintPageEnded is set properly and it would have to be a
form level field so it woould retain its values between calls.

====================
Clay Burch
Syncfusion, Inc.

Mar 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12324
by: Sharon | last post by:
Hi All. Is there a way to print the content of DataGridView, Without using Graphics.DrawString on every row? Thanks, Sharon.
3
10024
by: Arno Au | last post by:
Hi, how can I print the content of a datagridview in vb 2005? thanks for answers, Arno
3
7866
by: Marc | last post by:
Hallo, can someone tell me, how i could print a datagridview? I found in the whole net nothing :( bye
1
3880
by: vanderlej | last post by:
does anyone have code for PrintPreview and print of DataGridView? thanks
0
1836
by: Scotty | last post by:
Hi, Hope someone can help me I have a datagridview sith data Code below works fine if I print the data if there is only 1 page (without using '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If...
2
2647
by: Scotty | last post by:
Hi, Hope someone can help me I have a datagridview I want to print Code below works fine if I print the data if there is only 1 page (without using hasmore pages...
7
1802
devikacs
by: devikacs | last post by:
i am new to C# .net. i am trying to print the contents of a sql table on a form using datagridview. i've first printed the values of a select command on the datagridview. on clicking a button, the...
1
2565
OuTCasT
by: OuTCasT | last post by:
Can some one please tell me if this is the correct way to print data from a gridview because it gives me an error Private PrintGrid As DataGridViewPrint Private Sub btnPrint_Click(ByVal sender...
0
1319
by: murali007 | last post by:
Hi, i have to print datagridview content and some text using c# in windows application.. iam able to print datagridview print seperate and text seperate print .. but iam not able to print both...
1
1762
by: gleave | last post by:
Hi, below is the code i got to print a datagridview, the trouble is it also prints visible columns which i don't want to be printed. Does anyone have any ideas on how i could change the code so it...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
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...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
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
3232
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
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
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.