Solved

printing the contents of controls

Posted on 2009-06-29
5
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

726 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