Help with PrintDocument method...

m_g_Koz

New member
Joined
Apr 5, 2004
Messages
2
Hello..I am working through VB.Net and I hope someone can help me. A brief overview of the problem. I have a DataGrid that I am trying to print out to a PrintDocument. I have all my declaration working correctly and I get an output. However I am having a hard time getting my results to print out correctly. Basically, I cant get the HasMorePages parameter to work correctly. Using the code below, it hits an endless loop and consistently prints out the same five values. Say in my DataTable I have 9 rows..I want 2 pages...the first five values on the first page, the final four values on the second page. If I have 6 values, I want five on the first page, and one on the second page..etc. What am I a doing wrong? Is there a better method...thanks in advance

Code:
 Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim myDataRow As DataRow
        Dim i As Int32

        Y = PrintDocument1.DefaultPageSettings.Margins.Top + 20
        e.Graphics.DrawString("TSS TurboPay Report", TitleFont, Brushes.Black, X, Y)
        e.Graphics.DrawString("Orders prepared for: " & Me.ordDate.Text, TitleFont_A, Brushes.Black, X, Y + 40)
        e.Graphics.DrawString("Total amount of orders: " & Me.ordTotal.Text, TitleFont_A, Brushes.Black, X, Y + 70)
        e.Graphics.DrawString("Total Commissions: " & Me.totCom.Text, TitleFont_B, Brushes.Green, X, Y + 100)

        Y = Y + 160

        For Each myDataRow In DirectCast(Me.DataGrid1.DataSource, DataTable).Rows
            For i = 0 To myDataRow.ItemArray.Length - 1
                Dim strx As String
                strx = whatToSay(i) & myDataRow(i)
                e.Graphics.DrawString(strx, tableFont, Brushes.Black, X1, Y)
                Dim R As New RectangleF(X2, Y, W2, 80)
                Dim lines, cols As Integer
                e.Graphics.MeasureString(strx, tableFont, New SizeF(W2, 50), New StringFormat, cols, lines)
                Dim subitm As Integer, Yc As Integer
                Yc = Y
                Y = Y + lines * tableFont.Height + 5
                Y = Math.Max(Y, Yc)
            Next
           
           this just blows up, how can I get anything after the five record to 
           print successfully on page 2..and make sure the previous five 
           dont print again

            With PrintDocument1.DefaultPageSettings
                e.Graphics.DrawLine(Pens.Black, .Margins.Left, Y, .PaperSize.Width - .Margins.Right, Y)
                If Y > 0.95 * (.PaperSize.Height - .Margins.Bottom) Then
                    e.HasMorePages = True
                    Exit Sub
                End If
            End With
            Y = Y + 30
        Next

        e.HasMorePages = False
 
Back
Top