Printing using PrintDocument with many rows

Please Help in vb.net using print document, if i have 100 or more data in a particular table in database. How can i retrieve other informations or data and continously print with other data in the next page.

ex: in the 1st page 25 data and 2nd,3rd and 4th page are also 25 data

this is my code:

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

    Dim rect As New Rectangle(20, 10, CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Width), Panel2.Height)

    Static startPage As Integer = 0
    For p As Integer = startPage To pages.Count - 1
        Dim cell As New Rectangle(startX, startY, dgw.RowHeadersWidth, dgw.ColumnHeadersHeight)
        e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
        e.Graphics.DrawRectangle(Pens.Black, cell)
        startY += dgw.ColumnHeadersHeight
        For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
            cell = New Rectangle(startX, startY, dgw.RowHeadersWidth, dgw.Rows(r).Height)
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
            e.Graphics.DrawRectangle(Pens.Black, cell)

            startY += dgw.Rows(r).Height
        Next
        startX += cell.Width
        startY = rect.Bottom
        For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
            cell = New Rectangle(startX, startY, dgw.Columns(c).Width, dgw.ColumnHeadersHeight)
            e.Graphics.FillRectangle(New SolidBrush(SystemColors.ControlLight), cell)
            e.Graphics.DrawRectangle(Pens.Black, cell)
            e.Graphics.DrawString(dgw.Columns(c).HeaderCell.Value.ToString, dgw.Font, Brushes.Black, cell, sf)
            startX += dgw.Columns(c).Width
        Next
        startY = rect.Bottom + dgw.ColumnHeadersHeight

        For r As Integer = pages(p).startRow To pages(p).startRow + pages(p).rows - 1
            startX = 50 + dgw.RowHeadersWidth
            For c As Integer = pages(p).startCol To pages(p).startCol + pages(p).columns - 1
                cell = New Rectangle(startX, startY, dgw.Columns(c).Width, dgw.Rows(r).Height)
                e.Graphics.DrawRectangle(Pens.Black, cell)
                e.Graphics.DrawString(dgw(c, r).Value.ToString, dgw.Font, Brushes.Black, cell, sf)
                startX += dgw.Columns(c).Width
            Next
            startY += dgw.Rows(r).Height
        Next
        If p <> pages.Count - 1 Then
            startPage = p + 1
            e.HasMorePages = True
            Return
        Else
            startPage = 0
        End If
    Next

End Sub

Answers


OK, to start with, unless I very much mistake, PrintPage is raised every time a new page is called for. So all you have to do in the PrintPage event handler is write stuff for that particular page, not for all the pages at one time.

There shouldn't be any trouble retrieving other data as well as your primary data. The only time that would give trouble would be if your other data expanded the page vertically so that fewer than 25 of your data can be fit on it. But if you know that exactly 25 will fit, every time, this should be fairly easy.

If you're having specific problems, expand your question to state what the problems are, and I'll attempt to answer them.

You might also find this article useful.


Need Your Help

Fastest android server communication

java android protocols google-cloud-messaging

I am creating an android application to get information from a server as fast as possible.

What happens with Timers when PC wakes from sleep?

c# vb.net multithreading timer

I have a COM add-in for Office apps that performs an operation in the background every 30 minutes using a