Solved

printing the contents of controls

Posted on 2009-06-29
5
242 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:dizzycat
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:DanJourno
ID: 24736322
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
0
 

Author Comment

by:dizzycat
ID: 24736622
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.
0
 
LVL 5

Expert Comment

by:DanJourno
ID: 24736865
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.
0
 

Author Comment

by:dizzycat
ID: 24737855
Hi again DanJourno

Would you have any idea how to be able to preview what was going to be printed ie use a printpreviewdialougebox
0
 
LVL 5

Accepted Solution

by:
DanJourno earned 500 total points
ID: 24740360
Ive not had any experience of using printpreview. During development, I use a pdf printer to "print" into a PDF file and preview it there.

If you want to give the user PrintPreview functionality within the program, take a look at the PrintPreviewControl. I assume its in the standard toolbox.
Also, take a look at this website which gives some pointers:-
http://www.startvbdotnet.com/controls/printdialog.aspx

Hope that helps.
Dan
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now