how do i print 2 pages using vb.net without overlapping - vb.net

Hi i have problem on printing output on vb.net, I run-out of ideas in my mind. I hope you can help me. Here's my code:
If ComboBox1.Text = "Sampled" Then
For i As Integer = 0 To DataGridView1.RowCount - 1
If DataGridView1.Item(0, i).Value = "True" Then
Dim ContainerNo = DataGridView1.Item(18, i).Value
Dim TotalContainer = DataGridView1.Item(19, i).Value
'e.Graphics.DrawImage(image:=myBitmap2, point:=New Point(110, 40))
e.Graphics.DrawString("SAMPLED", font3, Brushes.Black, 120, 70)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 90, 140)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 90, 160)
e.Graphics.DrawString("SAMPLED BY:", font1, Brushes.Black, 170, 220)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 155, 235)
e.Graphics.DrawString("QC SAMPLER/DATE", font1, Brushes.Black, 160, 250)
e.Graphics.DrawString(ContainerNo, font1, Brushes.Black, 190, 138)
e.Graphics.DrawString(TotalContainer, font1, Brushes.Black, 280, 138)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 160, 158)
' e.HasMorePages = True
index += 1
If index <= x Then
e.HasMorePages = True
MessageBox.Show("True")
Else
e.HasMorePages = False
MessageBox.Show("false")
End If
End If
Next
here's my current output now
I want my output to be like this
1st page: 2 of 3 (2 is based from Dim ContainerNo = DataGridView1.Item(18, i).Value) and (3
is from Dim TotalContainer = DataGridView1.Item(19, i).Value)
2nd page: 3 of 3
Sadly my current output now is containerNo:2 and containerNo:3 are overlapping each other on both pages, I hope you can help me

Remove the loop. This function can be called multiple times in a single print job (HasMorePages controls whether it will be called again). Each time it's called you only print one page worth of data.
Dim i As Integer = index
If ComboBox1.Text = "Sampled" AndAlso DataGridView1.Item(0, i).Value = "True" Then
Dim ContainerNo = DataGridView1.Item(18, i).Value
Dim TotalContainer = DataGridView1.Item(19, i).Value
e.Graphics.DrawString("SAMPLED", font3, Brushes.Black, 120, 70)
e.Graphics.DrawString("CONTAINER No:________________OF________________", font1, Brushes.Black, 90, 140)
e.Graphics.DrawString("REMARKS:_______________________________________", font1, Brushes.Black, 90, 160)
e.Graphics.DrawString("SAMPLED BY:", font1, Brushes.Black, 170, 220)
e.Graphics.DrawString("____________________", font1, Brushes.Black, 155, 235)
e.Graphics.DrawString("QC SAMPLER/DATE", font1, Brushes.Black, 160, 250)
e.Graphics.DrawString(ContainerNo, font1, Brushes.Black, 190, 138)
e.Graphics.DrawString(TotalContainer, font1, Brushes.Black, 280, 138)
e.Graphics.DrawString(TextBox12.Text, font1, Brushes.Black, 160, 158)
End If
'Make sure index is declared OUTSIDE THE METHOD
index += 1
e.HasMorePages = index <= DataGridView1.RowCount - 1

Related

To fill color on a number of selected shapes by user choice

Dear Stackoverflow experts,
I am trying to write a code that will let me select a number of shapes, then prompt me what color should I fill up for each selected shape.
While the following code work well for me
Sub ChangeColorBasedonInput()
Dim myColor(1 To 10) As Long
Dim x As Integer
Dim z As Integer
Dim colorChoice As Integer
myColor(1) = RGB(77, 60, 47)
myColor(2) = RGB(207, 189, 155)
myColor(3) = RGB(192, 113, 86)
myColor(4) = RGB(232, 199, 103)
myColor(5) = RGB(174, 176, 179)
myColor(6) = RGB(164, 55, 37)
myColor(7) = RGB(237, 215, 157)
myColor(8) = RGB(123, 125, 128)
myColor(9) = RGB(230, 182, 164)
myColor(10) = RGB(70, 71, 73)
On Error Resume Next
z = ActiveWindow.Selection.ShapeRange.Count
For x = 1 To z
With ActiveWindow.Selection.ShapeRange(x)
colorChoice = InputBox("Please select the color you want for Shape " & x & ", from 1 - 10")
.Fill.ForeColor.RGB = myColor(colorChoice)
End With
Next x
End Sub
I realize its quite hassle to have to enter the color choice one by one, so I try to write the code below so that I only need to enter my fill color selection once (from the above array and it will automatically fill up the shapes for me. eg when I select 7, I hope it fills the shapes from myColor(7) onwards. But somehow the following codes doesn't work. Wonder if anyone could point out my mistake here.
Sub ChangeColorBasedon_EnterOnceOnly()
Dim myColor(1 To 10) As Long
Dim x As Integer
Dim z As Integer
Dim colorChoice As Integer
myColor(1) = RGB(77, 60, 47)
myColor(2) = RGB(207, 189, 155)
myColor(3) = RGB(192, 113, 86)
myColor(4) = RGB(232, 199, 103)
myColor(5) = RGB(174, 176, 179)
myColor(6) = RGB(164, 55, 37)
myColor(7) = RGB(237, 215, 157)
myColor(8) = RGB(123, 125, 128)
myColor(9) = RGB(230, 182, 164)
myColor(10) = RGB(70, 71, 73)
On Error Resume Next
z = ActiveWindow.Selection.ShapeRange.Count
colorChoice = InputBox("Please select the color you want for Shape " & x + 1 & ", from 1 - 10")
For x = 1 To z
With ActiveWindow.Selection.ShapeRange(x)
.Fill.ForeColor.RGB = myColor(colorChoice)
End With
colorChoice = colorChoice + x
Next x
End Sub
Just before Next x add
If colorChoice > UBound(myColor) Then colorChoice = LBound(myColor)
or you may exceed the bounds of the array if too many shapes are selected

PrintPreview is good, but prints empty

I'm adding a printing feature to my program. In print preview it shows all the data to be printed but it prints empty on paper.
Image is provided below
1st Preview
PrintPreview
This the code in PrintDocument1:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
If numpages = 1 Then
'e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
e.Graphics.DrawString("Date: " & Date.Now.ToLongDateString, fon, Brushes.Black, 590, 260)
e.Graphics.DrawString("Time: " & Date.Now.ToLongTimeString, fon, Brushes.Black, 590, 285)
e.Graphics.DrawString("Republic of the Philippines", Headerfon, Brushes.Black, 295, 40)
e.Graphics.DrawString("Cebu Technologcial University", Headerfon, Brushes.Black, 270, 70)
e.Graphics.DrawString("Daanbantayan Campuse", Headerfon, Brushes.Black, 305, 100)
e.Graphics.DrawString("( SUC Level IV, AACCUP Accredited and ISO 9001 Certified )", fon2, Brushes.Black, 220, 130)
e.Graphics.DrawString("Agujo, Daanbantayan, Cebu", fon, Brushes.Black, 332, 150)
e.Graphics.DrawString("http://daanbantayan.ctu.edu.ph", fon2, Brushes.SteelBlue, 80, 190)
e.Graphics.DrawString("Email: ctudaanbantayan#yahoo.com", fon2, Brushes.SteelBlue, 560, 190)
e.Graphics.DrawString("Fax No. (032) 437-8523", fon2, Brushes.SteelBlue, 560, 210)
e.Graphics.DrawString("Telephone No. (032) 437-8526", fon2, Brushes.SteelBlue, 80, 210)
e.Graphics.DrawLine(Pens.Black, 800, 240, 55, 240)
e.Graphics.DrawLine(Pens.Black, 800, 244, 55, 244)
e.Graphics.DrawImage(PictureBox1.Image, 60.0F, 52.0F, 120.0F, 120.0F)
e.Graphics.DrawImage(PictureBox2.Image, 610.0F, 50.0F, 120.0F, 120.0F)
End If
line1:
e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
While PrintI <= Form12.timelog.Rows.Count - 2
Dim ColReaderSpace As Integer = 1
For Each GridCol As DataGridViewColumn In Form12.timelog.Columns
If ColHeaderY >= 1200 Then
If PrintI >= 1 Then
ColCellY = 50
ColHeaderY = 50
ColCellSpace = 52
numpages += 1
e.HasMorePages = True
e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
'GoTo line1
Exit Sub
End If
End If
line2:
Dim Rect As New Rectangle(x, ColHeaderY, xWidth, yHeight)
Dim strings As New StringFormat
strings.Alignment = StringAlignment.Center
strings.LineAlignment = StringAlignment.Center
e.Graphics.FillRectangle(Brushes.WhiteSmoke, Rect)
e.Graphics.DrawRectangle(Pens.Black, Rect)
e.Graphics.DrawString(GridCol.HeaderText, fon, Brushes.Black, Rect)
If ColReaderSpace = 8 Then
ColHeaderY += 20
End If
ColHeaderY += 20
ColReaderSpace += 2
Next GridCol
For k As Integer = 0 To 7
Dim Rec As New Rectangle(y, ColCellY, CellWidth, yHeight)
e.Graphics.DrawRectangle(Pens.Black, Rec)
e.Graphics.DrawString(Form12.timelog.Rows(PrintI).Cells(k).Value.ToString, fon, Brushes.Black, CellHeight, ColCellSpace)
If ColReaderSpace = 10 Then
ColCellY += 20
ColCellSpace += 20
End If
ColCellY += 20
ColCellSpace += 20
ColReaderSpace += 1
Next
e.HasMorePages = False
PrintI += 1
End While
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf & "Please Make Sure There is Data to print", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
End Sub
This is the code in printing:
Private Sub print_Click(sender As Object, e As EventArgs) Handles print.Click
ColHeaderY = 320
ColCellY = 320
ColCellSpace = 320
PrintI = 0
numpages = 1
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
End If
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.TopMost = True
PrintPreviewDialog1.ShowDialog()
End Sub
I hope that someone can help me with this error.

e.graphics.hasmorepages = true doesn't print on the next page VB.NET

I am having hard time printing multiple pages on print document. It keeps printing everything on the first page even though I have new page set up in my code. Please help. Here is my code:`
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim yTop As Single = e.MarginBounds.Top
Dim leftmargin = e.MarginBounds.Left
Dim MyPen As New Pen(Color.Black, 1)
Dim regFont = New Font("Arial", 10)
Dim smallFont = New Font("Arial", 8)
Dim xsFont = New Font("Arial", 6)
Dim micrFont = New Font("MICR", 10)
Dim myprintfont = New Font("Arial", 12)
Dim Bigfont = New Font("Arial", 14, FontStyle.Bold)
Dim BldFnt = New Font("Arial", 9, FontStyle.Bold)
Dim fCtr As New StringFormat
fCtr.Alignment = StringAlignment.Center
Dim frt As New StringFormat
frt.Alignment = StringAlignment.Near
Dim flft As New StringFormat
flft.Alignment = StringAlignment.Far
'Dim mrc As String = ""
Dim lpos As Integer = My.Settings.ChkLeft
Dim blackPen As New Pen(Color.Black, 1)
Dim y As Integer = 0
Dim mrc As String = ""
Dim p As Integer = 0
Dim j As Integer = 0
Dim ct As Integer = dgChecks.SelectedRows.Count
Static totaChecksPrinted As Integer
If totaChecksPrinted < dgChecks.SelectedRows.Count - 1 Then
For Each row As DataGridViewRow In dgChecks.SelectedRows
st = row.Cells("CheckNumber").Value
Dim strSQL1 As String
strSQL1 = "SELECT * FROM QryCheckPrint WHERE CheckNumber In( " & st & ")"
If HQCon.State = ConnectionState.Closed Then HQCon.Open()
Dim ccmda As New Data.SqlClient.SqlCommand(strSQL1, HQCon)
Dim drc2 As Data.SqlClient.SqlDataReader
Dim HasRows As Boolean = False
drc2 = ccmda.ExecuteReader
If drc2.HasRows Then
While drc2.Read
y += My.Settings.ChkTop
e.Graphics.DrawString(drc2.Item("CorpName"), Bigfont, Brushes.Black, lpos, y, frt)
e.Graphics.DrawString(drc2.Item("CheckNumber"), Bigfont, Brushes.Black, lpos + 640, y + 1, frt)
y += 40
e.Graphics.DrawString(drc2.Item("Date"), smallFont, Brushes.Black, lpos + 640, y + 6, frt)
e.Graphics.DrawString("Date", myprintfont, Brushes.Black, lpos + 580, y + 5, frt)
e.Graphics.DrawLine(blackPen, lpos + 620, y + 25, lpos + 730, y + 25)
y += 60
e.Graphics.DrawString("Pay to the", smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString("order of", smallFont, Brushes.Black, lpos + 10, y, frt)
e.Graphics.DrawString(drc2.Item("Name"), myprintfont, Brushes.Black, lpos + 90, y - 10, frt)
If Not IsDBNull(drc2.Item("Amount")) Then
e.Graphics.DrawString("$" & Format(drc2.Item("Amount"), "n2"), myprintfont, Brushes.Black, lpos + 640, y - 5, frt)
End If
y += 15
e.Graphics.DrawLine(blackPen, lpos + 50, y, lpos + 640, y)
e.Graphics.DrawLine(blackPen, lpos + 50, y + 30, lpos + 640, y + 30)
e.Graphics.DrawLine(blackPen, lpos + 640, y - 25, lpos + 640, y)
e.Graphics.DrawString("**" & Functions.AmountInWords(drc2.Item("Amount")) & "**", regFont, Brushes.Black, lpos + 100, y + 10, frt)
y += 40
e.Graphics.DrawString(drc2.Item("BankName"), smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString(drc2.Item("BankAddress"), smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString(drc2.Item("City") & " " & drc2.Item("State") & ", " & drc2.Item("Zip"), smallFont, Brushes.Black, lpos, y, frt)
mrc = "o" & drc2.Item("CheckNumber") & "o T" & drc2.Item("ABA") & "T" & drc2.Item("AccountNumber") & "o"
y += 30
e.Graphics.DrawString(drc2.Item("Description"), smallFont, Brushes.Black, lpos + 80, y, frt)
e.Graphics.DrawString("For", regFont, Brushes.Black, lpos + 30, y, frt)
e.Graphics.DrawString("By", regFont, Brushes.Black, lpos + 480, y, frt)
e.Graphics.DrawString(drc2.Item("StoreCode"), xsFont, Brushes.Black, lpos + 400, y, frt)
e.Graphics.DrawLine(blackPen, lpos + 30, y + 20, lpos + 440, y + 20)
e.Graphics.DrawLine(blackPen, lpos + 500, y + 20, lpos + 720, y + 20)
y = y + My.Settings.MicrPos
e.Graphics.DrawString(mrc, micrFont, Brushes.Black, lpos + 100, y, frt)
y = y + My.Settings.ChkBottom
j += 1
totaChecksPrinted += 1
End While
End If
drc2.Close()
ccmda = Nothing
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 0
End If
Next
End If
If HQCon.State = ConnectionState.Open Then HQCon.Close()
End Sub`
Each time the PrintPage event is raised, you should print only the data that goes on that page. If you have more data to print, set HasMorePages to True and then Exit Sub. Then the PrintPage method will be called again for the next page. You will need to keep track of what you have already printed between pages somehow. Something like this:
'Class level variable perhaps
Dim pageNumberToPrint As Integer = 1
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Print the data that goes on the current page (pageNumberToPrint)
If pageNumberToPrint = 1 Then
'Print page number 1
Else
If pageNumberToPrint = 2 Then
'Print page number 2
Else
'Print page number 3
End If
End If
'If you're at the end of the page, and still have more data to print
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 0
pageNumberToPrint += 1
Exit Sub 'The PrintPage event handler will be raised again
End If
End Sub

Spacing out 4 areas in a defined width

Hey all i am trying to add 4 boxes to an image that's 1280 x 720.
I am wanting to add the boxes to the top of the image but space them out evenly across the 1280 width.
Dim g As Graphics = Graphics.FromImage(image)
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(3, 7, 270, 25)) 'The transparent square for Date
g.DrawString(Format(DateTime.Now, "MM/dd/yyyy HH:mm:ss tt"), New Font("Arial", 18), Brushes.Black, New PointF(3, 5)) 'The date
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(350, 7, 170, 25)) 'The transparent square for Latitude
g.DrawString("Lat: " & "30.976154", New Font("Arial", 18), Brushes.Black, New PointF(352, 5))
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(670, 7, 180, 25)) 'The transparent square for longitude
g.DrawString("Lng: " & "33.351328", New Font("Arial", 18), Brushes.Black, New PointF(672, 5))
g.FillRectangle(New SolidBrush(Color.FromArgb(90, 255, 255, 255)), New Rectangle(970, 7, 120, 25)) 'The transparent square for MPH
g.DrawString("MPH: " & "000", New Font("Arial", 18), Brushes.Black, New PointF(972, 5))
g.Dispose()
However i haven't found a sure fire way to making them even across the screen since each rectangle/text is a different width than the ones around it.
Any ideas, thoughts would be great!
Simply divide the width by the number of labels. Here's some pseudocode:
const int NUM_LABELS = 4;
int divWidth = width / NUM_LABELS;
int i;
for i = 0 to (NUM_LABELS - 1)
FillRect(i * divWidth, LABEL_HEIGHT, (i + 1) * divWidth, 0); // or whatever you want to do
MoveTo (i * divWidth, LABEL_HEIGHT);
DrawString("some string");

Printing More than One Page with a Loop From DataGridView

I have a code am working that prints a sales report from a DataGridView. The Code prints well but can only print one page when the Value of the control Variable I goes out of the printable area. I really Need your help. Below is the code.
Dim fntAddress As New Font("Comic Sans MS", 10, FontStyle.Regular)
Dim fntHeader As New Font("Calibri", 20, FontStyle.Bold)
Dim fntBodyText As New Font("Calibri", 12, FontStyle.Regular)
Dim fntHeaderText As New Font("Calibri", 13, FontStyle.Bold)
Dim strTotalSale = txtTotal.Text
e.Graphics.DrawString("SFC POINT OF SALE AND INVENTORY MANAGEMENT", fntHeader, Brushes.Black, 100, 0)
e.Graphics.DrawString("GENERATED SALES REPORT", New Font("Calibri", 18, FontStyle.Bold), Brushes.Black, 250, 30)
Dim strDateString As String = ""
If mtbStartDate.Text = " / /" Or mtbEndDate.Text = " / /" Then
strDateString = ""
ElseIf mtbStartDate.Text = mtbEndDate.Text Then
strDateString = "Report For Date Of : " & mtbStartDate.Text
ElseIf mtbStartDate.Text <> mtbEndDate.Text Then
strDateString = "Report For Dates Of : " & mtbStartDate.Text & " - " & mtbEndDate.Text
End If
e.Graphics.DrawString(strDateString, New Font("Courier New", 15, FontStyle.Regular), Brushes.Black, 5, 70)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, 100, 770, 35))
e.Graphics.DrawString("Item Barcode", fntHeaderText, Brushes.Black, 10, 107)
e.Graphics.DrawString("Item Name", fntHeaderText, Brushes.Black, 160, 107)
e.Graphics.DrawString("Quantity", fntHeaderText, Brushes.Black, 360, 107)
e.Graphics.DrawString("Unit Cost", fntHeaderText, Brushes.Black, 450, 107)
e.Graphics.DrawString("Sub Total", fntHeaderText, Brushes.Black, 560, 107)
e.Graphics.DrawString("Date of Sale", fntHeaderText, Brushes.Black, 660, 107)
Dim RowCount As Integer = dgvSales.Rows.Count - 1
Static i As Integer = 139
Dim x1 = 10
Dim x2 = 700
Dim y1 = 155
Dim n As Integer = 0
While n < RowCount
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, i - 5, 770, 35))
e.Graphics.DrawString(dgvSales.Rows(n).Cells(0).Value, fntBodyText, Brushes.Black, 16, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(1).Value, fntBodyText, Brushes.Black, 160, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(2).Value, fntBodyText, Brushes.Black, 360, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(3).Value, fntBodyText, Brushes.Black, 450, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(4).Value, fntBodyText, Brushes.Black, 560, i)
e.Graphics.DrawString(dgvSales.Rows(n).Cells(5).Value, fntBodyText, Brushes.Black, 660, i)
i = i + 35
n = n + 1
End While
e.HasMorePages = False
e.Graphics.DrawLine(Pens.Black, 150, 100, 150, i - 5)
e.Graphics.DrawLine(Pens.Black, 350, 100, 350, i - 5)
e.Graphics.DrawLine(Pens.Black, 440, 100, 440, i - 5)
e.Graphics.DrawLine(Pens.Black, 550, 100, 550, i - 5)
e.Graphics.DrawLine(Pens.Black, 650, 100, 650, i - 5)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(340, i + 40, 174, 50))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(341, i + 41, 172, 48))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(342, i + 42, 170, 46))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(343, i + 43, 168, 44))
e.Graphics.DrawString("Total Sales.", New Font("Times New Roman (Headings CS)", 22, FontStyle.Bold), Brushes.Black, 341, i + 47)
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(525, i + 40, 250, 50))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(526, i + 41, 248, 48))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(527, i + 42, 246, 46))
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(528, i + 43, 244, 44))
e.Graphics.DrawString(strTotalSale, New Font("Times New Roman (Headings CS)", 23, FontStyle.Bold), Brushes.Black, 538, i + 47)
Honestly, I haven't written one line of VB code in my life, but the printing mechanism at a glance seems extremely similar to the Java Printing API.
You have to mark the fact that the document you're printing has more pages yourself, using e.hasMorePages = True.
Here is a working example:
http://www.dreamincode.net/forums/topic/139447-help-with-hasmorepages-please/
Take into consideration the fact that you must always know the place you left off on the former page. Here is another, more realistic example: http://www.dreamincode.net/forums/topic/128639-ehasmorepages-and-logic-to-use-it/
Hope you understand that it could be complex because you have to calculate fontheight, papersize height, etc.
In this case, consider that there would be a fixed setting
Dim nMaxLine as Integer = 30 ' ----------> lines count per page
Dim x as Integer
Dim n as Integer
For x = 0 to datagridview.rows.count - 1
' ..
e.Graphics.DrawString( ... )
' ..
n += 1
If n > nMaxLine Then
e.hasMorePages = True
n = 0
End If
Next