Link to home
Start Free TrialLog in
Avatar of dbote
dboteFlag for United States of America

asked on

Multiple pate PrintDocument with a DataTable

I have a VERY simple report to develop and thought that developing it would be very simple.  Unfortunately just the opposite has turned out to be true because I am unfamiliar with the PrintDocument object.

I retrieve a DataReader from the database and Loop through the records.  Each record I want to add a line to the report.  With each line I add a specific about of space which actually prints well.  The problem is that the code prints on one page,each page on top of the other.  

BTW, I am always open to a BETTER way if the PrintDocument is not how I should be doing simple reports.

Thanks in advance,

db
Private Sub pdReport_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdReport.PrintPage
 
        'Get the data
        Dim cn As New OleDbConnection(ConfigurationManager.ConnectionStrings("TheConnection").ConnectionString)
        cn.Open()
        Dim cm As New OleDbCommand("Select Name,City,State from Suppliers", cn)
        cm.CommandType = CommandType.StoredProcedure
        Dim rdr As OleDbDataReader = cm.ExecuteReader
        'Set the starting vertical location for printing
        Dim y As Integer = 55
        'Loop through the records
        Do While rdr.Read
            'Print each column at the specific location
            e.Graphics.DrawString(rdr.Item("Name").ToString, printFont, Brushes.Black, 100, y + 15)
            e.Graphics.DrawString(rdr.Item("City").ToString, printFont, Brushes.Black, 250, y + 15)
            e.Graphics.DrawString(rdr.Item("State").ToString, printFont, Brushes.Black, 425, y + 15)
            'Increment y so the next line will print after the current line
            y += 15
            'Evaluate y to see if a new page is needed
            If y >= 1000 Then
                e.HasMorePages = True
                'Reset where y starts 
                y = 55
            End If
        Loop
        'No more records end the document
        e.HasMorePages = False
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>BTW, I am always open to a BETTER way if the PrintDocument is not how I should be doing simple reports.

If reports is what you are doing then why not use the Crystal Reports which come built in with the .NET?
Avatar of dbote

ASKER

I'm guessing that you are suggesting the ReportViewer control?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dbote

ASKER

I appreciate the help, thank you.