-
Jun 10th, 2019, 12:21 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Generating a sales ticket from a datagridview in VB.NET (help).
Hello!
I just got my first Thermal printer, I wrote a program in VB.NET that already shows me a datagridview with the data I'd like to pass to the ticket, I want to print only some columns of the datagridview, I've already installed the printer's driver correctly, it even prints the test page OK. then I added a "PrintDocument" object into my form, I guess that is what should be done, then put some code in this object to print the ticket. since the beginning I don't want to use crystal report or anything like that, I want to use the native tools that come with VS that's why I used "PrintDocument" object.
My question is... Am I in the right track? since I'm a beginner in programming, perhaps I might be going in the wrong direction.
All comments will be appreciated.
thank you.
-
Jun 10th, 2019, 12:49 AM
#2
Re: Generating a sales ticket from a datagridview in VB.NET (help).
You are indeed on the right track. You don;t put code in a PrintDocument though, any more than you put code in a Button or TextBox. Your code is put in your form. That's it, that's all. It goes in event handlers, which are part of the form. If you have handled the PrintPage event of your PrintDocument, the PrintDocument instance will raise its PrintPage event and then invoke that method of your form.
If you want to learn more about using a PrintDocument, try here:
http://www.vbforums.com/showthread.p...rinting-in-NET
To do the printing you want, you'll need to learn a bit about GDI+, which is what you use the do the drawing, i.e. the Graphics class is the core of GDI+. You'll then need to do a bit of mathematics to determine exactly where to draw all the text and any other bits and pieces you want drawn
-
Jun 11th, 2019, 11:13 PM
#3
Thread Starter
Hyperactive Member
Re: Generating a sales ticket from a datagridview in VB.NET (help).
Hi jmc!
Thanks for the guidance, I've been reading on it.
So I see that I have to use the "PrintDocument" object along with it's events, like:
.BeginPrint
.QueryPageSettings
.PrintPage
And then stick the different codes in those, one question... Do I have to import any namespaces in my form? I see I might have to use Pagesettings & PrintDocument namespaces.
Last edited by Spybot; Jun 11th, 2019 at 11:18 PM.
-
Jun 11th, 2019, 11:44 PM
#4
Re: Generating a sales ticket from a datagridview in VB.NET (help).
PageSettings and PrintDocument are classes, not namespaces. Importing a namespace is not something that you ever HAVE to do. I suggest that you follow the Blog link in my signature below and check out my post on Assemblies & Namespaces to help your understanding in this area.
-
Jun 12th, 2019, 02:10 PM
#5
Thread Starter
Hyperactive Member
Re: Generating a sales ticket from a datagridview in VB.NET (help).
Hi jmc!
I've already got some code to start with...
The idea is to print something like this:
I've only achieved to print the header of the ticket (It prints OK).
the part where the items are pulled from the datagridview still needs a little bit of refinement.
this is what my DGV looks like:
I think I set up the column numbers of my DGV correctly, however I'm getting this error message.
Last edited by Spybot; Jun 12th, 2019 at 02:31 PM.
-
Jun 12th, 2019, 02:22 PM
#6
Thread Starter
Hyperactive Member
Re: Generating a sales ticket from a datagridview in VB.NET (help).
Hi jmc!
I've already got some code to start with... by the way my Printer uses 58mm paper roles.
The idea is to print something like this:
I've only achieved to print the header of the ticket (It prints OK).
the part where the items are pulled from the datagridview still needs a little bit of refinement.
this is what my DGV looks like:
I think ,I set up the column numbers of my DGV correctly, however I'm getting this error message.
I only have 2 pieces of code, one in my "PrintPage" even and the other in my button3.
The code for my button:
VB.NET Code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btn_cobrar.Click Printticket.Print() End sub
My main printing code:
VB.NET Code:
Private Sub Printticket_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Printticket.PrintPage Dim fuente As System.Drawing.Font = New Font("Courier New", 15) Dim topMargin As Double = e.MarginBounds.Top Dim yPos As Double = 0 Dim count As Integer = 0 Dim texto As String = "" Dim Quantity As Integer = 0 Dim description As String = "" Dim price As Decimal = 0 Dim Lvalue As Integer Dim tabulation As String = "" Dim compensator As Integer = 0 Dim total As Decimal = 0 Dim lineTotal As Integer ' Prints the header yPos = 40 Dim printFont As System.Drawing.Font = New Font("Courier New", 10) e.Graphics.DrawString("Tiket N. 155", printFont, Brushes.Black, 10, 100) e.Graphics.DrawString("Date: " & Date.Now, printFont, Brushes.Black, 10, 120) e.Graphics.DrawString("---------------------------------", printFont, Brushes.Black, 10, 140) ' Goes through out the Rows in the DataGridView colecting data For Each row As DataGridViewRow In DataGridView1.Rows Quantity = row.Cells(2).Value.ToString : description = row.Cells(0).Value : price = row.Cells(4).FormattedValue Lvalue = Len(row.Cells(4).FormattedValue.ToString) compensator = Len(row.Cells(0).Value) tabulation = StrDup(22 - compensator, " ") ' Configures the line texto = Quantity & " " & description & tabulation & StrDup(8 - Lvalue, " ") & price ' Calculates the posición where the line gets printed yPos = 120 + topMargin + (count * printFont.GetHeight(e.Graphics)) ' Prints the line using Graphics object If Not row.IsNewRow Then e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 10, yPos) total += price End If count += 1 Next yPos += 10 e.Graphics.DrawString(" _______", printFont, System.Drawing.Brushes.Black, 10, yPos) Dim Sum_amount As Integer = 0 Sum_amount = Len(total.ToString) lineTotal = StrDup(28 - Sum_amount, ".") yPos += 20 e.Graphics.DrawString("Total" & lineTotal & total, printFont, System.Drawing.Brushes.Black, 10, yPos) yPos += 30 e.Graphics.DrawString("¡Thank you for your visit!", printFont, Brushes.Black, 10, yPos + 20) e.HasMorePages = False End Sub
Last edited by Spybot; Jun 12th, 2019 at 02:28 PM.
-
Jun 12th, 2019, 07:24 PM
#7
Re: Generating a sales ticket from a datagridview in VB.NET (help).
The error message suggests that you are trying to convert data from the first column in the grid into Integers, which is obviously wrong. You ought to concentrate on being able to get the data you need out of the grid first, before trying to print it.
By the way, error messages are text so you should post them as text rather than as pictures. In this case particularly, it could have been an issue that you posted a picture because it's also in another language that you didn't bother to translate for us. Fortunately my rudimentary Spanish is good enough that I could work out what it meant but, if I had wanted to copy and paste it into an online translator, I wouldn't have been able to.
-
Jun 12th, 2019, 07:53 PM
#8
Thread Starter
Hyperactive Member
Re: Generating a sales ticket from a datagridview in VB.NET (help).
I apologize for posting the errors as images, I didn't think of it, it won't happen again.
On the other hand you're right, I need some kind of preview window before sending stuff to print, but fist I'd need to save those lines somewhere. How do I do that?
Last edited by Spybot; Jun 12th, 2019 at 08:50 PM.
-
Jun 12th, 2019, 08:50 PM
#9
Re: Generating a sales ticket from a datagridview in VB.NET (help).
Originally Posted by Spybot
I'm sure the column numbers are the correct ones, as well as the data types.
We always think we did the right thing. That's why we did it in the first place. Obviously something is wrong, so being sure it's right doesn't help. That's why we debug. Place a breakpoint at the top of the code and step through it line by line, examining the state as you go. You'll then be able to see what is actually happening rather than what you think is happening.
I would also suggest that you turn Option Strict On if you haven't already. It will help avoid issues like this by flagging places where you are making assumptions about data types. It won't prevent you making the wrong casts and conversions but at least it makes you think about what you should be doing where a cast or conversion is required.
I also can't recommend enough that you get rid of those colons and put separate lines of code on separate lines. Putting multiple lines of code on one line is a great way to make issues harder to find.
-
Jun 12th, 2019, 08:58 PM
#10
Re: Generating a sales ticket from a datagridview in VB.NET (help).
Originally Posted by Spybot
On the other hand you're right, I need some kind of preview window before sending stuff to print, but fist I'd need to save those lines somewhere. How do I do that?
You can use a PrintPreviewDialog and then the same code will be used to create the preview and do the final printing. That's not what I was talking about though. What I meant was that you should stick to tackling one problem at a time, which makes finding issues easier. You're currently trying to print data that you can;t even get out of a grid properly so, if there's an issue, it's hard to work out which part is going wrong. You should write code to get the data you want out of the grid first and confirm that either using the debugger or display a message box or write to a file or whatever. Once you know that you have the right data, then start writing code to print that data. Doing too much at once is a great way to make your life more difficult.
-
Jun 12th, 2019, 09:18 PM
#11
Thread Starter
Hyperactive Member
Re: Generating a sales ticket from a datagridview in VB.NET (help).
Thank very much for the advice.
I got rid of the colons, separated the lines, then I realized my DGV had hidden columns, that is why my column numbers were wrong, fixing all that I finally printed the whole ticket. now I need to test the PrintPreviewDialog thing...
-
Jun 12th, 2019, 11:58 PM
#12
Thread Starter
Hyperactive Member
Re: Generating a sales ticket from a datagridview in VB.NET (help).
I'm back...
I've made a couple of adjustments to my code so this is what my preview looks like now:
I clearly have a problem with my Header (the name of the company) I pulled this one from a textbox that has a Maxlength = 44 chars, in my preview it only shows 21 Chars, how do show the rest of the characters (if there are more than 21) in the next line?
the actual code:
VB.NET Code:
Private Sub Printticket_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Printticket.PrintPage Dim Header_font As System.Drawing.Font = New Font("Courier New", 10) Dim topMargin As Double = e.MarginBounds.Top Dim yPos As Double = 0 Dim count As Integer = 0 Dim texto As String = "" Dim Quantity As Integer = 0 Dim description As String = "" Dim price As Decimal = 0 Dim Lvalue As Integer Dim tabulation As String = "" Dim Item_name As Integer = 0 Dim total As Decimal = 0 Dim line_Total As String Try ' Printes the header yPos = 10 Dim printFont As System.Drawing.Font = New Font("Courier New", 7) e.Graphics.DrawString(TextBox1.Text, Header_font, Brushes.Black, 5, 10) e.Graphics.DrawString("Telphone: " & TextBox3.Text, printFont, Brushes.Black, 5, 30) e.Graphics.DrawString("Ticket N. 155", printFont, Brushes.Black, 5, 40) e.Graphics.DrawString("Date:", printFont, Brushes.Black, 5, 50) e.Graphics.DrawString(Date.Now, printFont, Brushes.Black, 5, 60) e.Graphics.DrawString("------------------------------", printFont, Brushes.Black, 5, 70) ' Goes through out the Rows in the DataGridView colecting data For Each row As DataGridViewRow In DataGridView1.Rows Quantity = row.Cells(4).Value description = row.Cells(2).Value price = row.Cells(6).FormattedValue Lvalue = Len(row.Cells(6).FormattedValue.ToString) Item_name = Len(row.Cells(2).Value) tabulation = StrDup(19 - Item_name, " ") ' Configures the line (#items--descrption--price) texto = Quantity & " " & description.ToString & tabulation & StrDup(8 - Lvalue, " ") & price ' Calculates the posición where the line gets printed yPos = -15 + topMargin + (count * printFont.GetHeight(e.Graphics)) ' Prints the line using Graphics object If Not row.IsNewRow Then e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 5, yPos) total += price End If count += 1 Next yPos += 2 e.Graphics.DrawString(" ________", printFont, System.Drawing.Brushes.Black, 5, yPos) Dim Sum_amount As Integer = 0 Sum_amount = Len(tbox_importe.Text) line_Total = StrDup(23 - Sum_amount, ".") yPos += 12 e.Graphics.DrawString("Total " & line_Total & tbox_importe.Text, printFont, System.Drawing.Brushes.Black, 5, yPos) yPos += 2 e.Graphics.DrawString(" ¡Thank you for your", printFont, Brushes.Black, 5, yPos + 20) e.Graphics.DrawString(" visit!", printFont, Brushes.Black, 5, yPos + 30) e.HasMorePages = False Catch ex As Exception MsgBox(ex.Message) End Try End Sub
-
Jun 13th, 2019, 12:46 AM
#13
Re: Generating a sales ticket from a datagridview in VB.NET (help).
Read up on the documentation for the DrawString method. It allows you to specify that text should be wrapped. I think that it's done via a StringFormat object but you can look into that.
You might also want to use the MeasureString method to determine the size of the area the text will occupy. You'll presumably want to move the rest of the text up or down based on whether the company name covers one line or two.
-
Jun 13th, 2019, 11:18 PM
#14
Thread Starter
Hyperactive Member
Re: [RESOLVED] Generating a sales ticket from a datagridview in VB.NET (help).
Thank you very much for the feedback, the goal of this threat has been achieved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|