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
Solved

printing the contents of controls

Posted on 2009-06-29
5
264 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a starter for ETL protocol? 4 65
Web Form VB.Net  import CSV 4 43
System.net.Sockets Error 5 26
vb.net Configuration, settings file location? 8 29
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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