Link to home
Start Free TrialLog in
Avatar of dizzycat
dizzycat

asked on

printing the contents of controls

Hi Experts

In vb2008 express edition i have a datagridview which displays the contents of a query on an access database, what i want to do is select certain records(rows) from the datagridview and print them in the form of an invoice eg:

The DataGridView

CustomerName | ID    |  OrderNo |   Details           | Price        |  

smith                  001    601            Lawnmower      £89.99
smith                  001    602            Hedgetrimmer     £59.99
smith                  001    603            Spade                £12.99
_____________________________________________________________________
The Invoice

Customer Name         smith
ID                                001

OrderNo     Details                Price        

601             Lawnmower      £89.99
602             Hedgetrimmer     £59.99
603             Spade                 £12.99

                         
                     subtotal          _______
                     tax                 ________
                grand total          ________
________________________________________________________________

As i have shown above this code will need to be able to work out the subtotal, tax and a grand total.  
Hope somebody can help.
Avatar of DanJourno
DanJourno
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

You can draw your own print document and send it to the printer.

Its a little complicated and you may use quite a bit of paper in the process, but its quite simple once you've done it a few times.

First, you need a command button (or similar) and here is the code for it:-
            Dim doc As New PrintDocument()

' I use these to track what row im currently on. They are global integers
            PrintRowPosition = 0
            PrintPageNo = 1

            AddHandler doc.PrintPage, AddressOf Me.PrintFunc

            Dim dlgSettings As New PrintDialog()
            dlgSettings.Document = doc

            If dlgSettings.ShowDialog() = DialogResult.OK Then
                doc.Print()
            End If
===========
Then you need "printfunc" which is called repeatedly until you set e.hasmorepages to false

    Private Sub PrintFunc(ByVal sender As Object, ByVal e As PrintPageEventArgs)
dim StartingRow as integer = PrintRowPosition
dim Rect as new Rectangle
for PrintRowPosition  = Startingrow to List1.rows.count-1
'Here, calculate the position of the cell you are drawing, and set the rect x, y, width and height to the correct sizes.
'work out if you've hit the bottom of the page by looking at the rect.bottom variable and comparing it with the e.Marginbounds.bottom.
'If you've reached the bottom, set e.hasmorepages to true, and then exit sub. It will be called again automatically to draw the next page.
'then call this to draw the text
                    e.Graphics.DrawString(CELL_TEXT, FONT, Brushes.Black, Rect)
next

e.hasmorepages = false
End sub

====

just keep a running total and draw it at the end of the report.

Sorry if the coding has bugs. I wrote it directly into EE instead of testing it in .net
Avatar of dizzycat
dizzycat

ASKER

I understand what you are doing here, although going through every cell getting its size and working out where to print it does seem very involved, but i will give it a go and get back to you.
Just use the loop and work out the row position by doing TextHeight x RowNumber.

The list I use gives me the cell size, so when I print, i get the list to automatically expand the columns to fit all the text, then i take the cell size from that to ensure it all fits.
Hi again DanJourno

Would you have any idea how to be able to preview what was going to be printed ie use a printpreviewdialougebox
ASKER CERTIFIED SOLUTION
Avatar of DanJourno
DanJourno
Flag of United Kingdom of Great Britain and Northern Ireland 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