Print datagridview How to...

S

Scotty

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).Value)
e.Graphics.DrawString(strRef3, MyFont, Borstel, 15, IntPrintHo)

'Printing text
strArtikel4 = CStr(dgvFacturenInfo.Rows(row.Index).Cells(3).Value)
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).Value)), 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).Value)), 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).Value)))
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
 
C

ClayB

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top