Results 1 to 14 of 14

Thread: [RESOLVED] Generating a sales ticket from a datagridview in VB.NET (help).

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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

  3. #3

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  5. #5

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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:
    Name:  Printed Ticket.PNG
Views: 1213
Size:  171.0 KB

    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:
    Name:  DGV.jpg
Views: 1036
Size:  8.7 KB

    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.

  6. #6

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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:
    Name:  Printed Ticket.PNG
Views: 1213
Size:  171.0 KB

    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:
    Name:  DGV.jpg
Views: 1036
Size:  8.7 KB

    I think ,I set up the column numbers of my DGV correctly, however I'm getting this error message.
    Name:  error.jpg
Views: 1076
Size:  21.7 KB

    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:
    1. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btn_cobrar.Click
    2.             Printticket.Print()
    3.     End sub


    My main printing code:
    VB.NET Code:
    1. Private Sub Printticket_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Printticket.PrintPage
    2.         Dim fuente As System.Drawing.Font = New Font("Courier New", 15)
    3.         Dim topMargin As Double = e.MarginBounds.Top
    4.         Dim yPos As Double = 0
    5.         Dim count As Integer = 0
    6.         Dim texto As String = ""
    7.         Dim Quantity As Integer = 0
    8.         Dim description As String = ""
    9.         Dim price As Decimal = 0
    10.         Dim Lvalue As Integer
    11.         Dim tabulation As String = ""
    12.         Dim compensator As Integer = 0
    13.         Dim total As Decimal = 0
    14.         Dim lineTotal As Integer
    15.  
    16.         ' Prints the header
    17.         yPos = 40
    18.         Dim printFont As System.Drawing.Font = New Font("Courier New", 10)
    19.         e.Graphics.DrawString("Tiket N. 155", printFont, Brushes.Black, 10, 100)
    20.         e.Graphics.DrawString("Date: " & Date.Now, printFont, Brushes.Black, 10, 120)
    21.         e.Graphics.DrawString("---------------------------------", printFont, Brushes.Black, 10, 140)
    22.  
    23.         ' Goes through out the Rows in the DataGridView colecting data
    24.         For Each row As DataGridViewRow In DataGridView1.Rows
    25.             Quantity = row.Cells(2).Value.ToString : description = row.Cells(0).Value : price = row.Cells(4).FormattedValue
    26.             Lvalue = Len(row.Cells(4).FormattedValue.ToString)
    27.             compensator = Len(row.Cells(0).Value)
    28.             tabulation = StrDup(22 - compensator, " ")
    29.  
    30.             ' Configures the line
    31.             texto = Quantity & "    " & description & tabulation & StrDup(8 - Lvalue, " ") & price
    32.             ' Calculates the posición where the line gets printed
    33.             yPos = 120 + topMargin + (count * printFont.GetHeight(e.Graphics))
    34.             ' Prints the line using Graphics object
    35.             If Not row.IsNewRow Then
    36.                 e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 10, yPos)
    37.                 total += price
    38.             End If
    39.             count += 1
    40.         Next
    41.         yPos += 10
    42.         e.Graphics.DrawString("                           _______", printFont, System.Drawing.Brushes.Black, 10, yPos)
    43.         Dim Sum_amount As Integer = 0
    44.         Sum_amount = Len(total.ToString)
    45.         lineTotal = StrDup(28 - Sum_amount, ".")
    46.         yPos += 20
    47.         e.Graphics.DrawString("Total" & lineTotal & total, printFont, System.Drawing.Brushes.Black, 10, yPos)
    48.         yPos += 30
    49.         e.Graphics.DrawString("¡Thank you for your visit!", printFont, Brushes.Black, 10, yPos + 20)
    50.         e.HasMorePages = False
    51.  
    52.     End Sub
    Last edited by Spybot; Jun 12th, 2019 at 02:28 PM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  8. #8

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Generating a sales ticket from a datagridview in VB.NET (help).

    Quote Originally Posted by Spybot View Post
    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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Generating a sales ticket from a datagridview in VB.NET (help).

    Quote Originally Posted by Spybot View Post
    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.

  11. #11

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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...

  12. #12

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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:
    Name:  Printed Ticket.PNG
Views: 1260
Size:  37.8 KB

    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:
    1. Private Sub Printticket_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Printticket.PrintPage
    2.         Dim Header_font As System.Drawing.Font = New Font("Courier New", 10)
    3.         Dim topMargin As Double = e.MarginBounds.Top
    4.         Dim yPos As Double = 0
    5.         Dim count As Integer = 0
    6.         Dim texto As String = ""
    7.         Dim Quantity As Integer = 0
    8.         Dim description As String = ""
    9.         Dim price As Decimal = 0
    10.         Dim Lvalue As Integer
    11.         Dim tabulation As String = ""
    12.         Dim Item_name As Integer = 0
    13.         Dim total As Decimal = 0
    14.         Dim line_Total As String
    15.  
    16.         Try
    17.         ' Printes the header
    18.         yPos = 10
    19.         Dim printFont As System.Drawing.Font = New Font("Courier New", 7)
    20.         e.Graphics.DrawString(TextBox1.Text, Header_font, Brushes.Black, 5, 10)
    21.         e.Graphics.DrawString("Telphone: " & TextBox3.Text, printFont, Brushes.Black, 5, 30)
    22.  
    23.         e.Graphics.DrawString("Ticket N. 155", printFont, Brushes.Black, 5, 40)
    24.         e.Graphics.DrawString("Date:", printFont, Brushes.Black, 5, 50)
    25.         e.Graphics.DrawString(Date.Now, printFont, Brushes.Black, 5, 60)
    26.         e.Graphics.DrawString("------------------------------", printFont, Brushes.Black, 5, 70)
    27.  
    28.         ' Goes through out the Rows in the DataGridView colecting data
    29.         For Each row As DataGridViewRow In DataGridView1.Rows
    30.             Quantity = row.Cells(4).Value
    31.             description = row.Cells(2).Value
    32.             price = row.Cells(6).FormattedValue
    33.             Lvalue = Len(row.Cells(6).FormattedValue.ToString)
    34.             Item_name = Len(row.Cells(2).Value)
    35.             tabulation = StrDup(19 - Item_name, " ")
    36.  
    37.             ' Configures the line (#items--descrption--price)
    38.             texto = Quantity & "  " & description.ToString & tabulation & StrDup(8 - Lvalue, " ") & price
    39.             ' Calculates the posición where the line gets printed
    40.             yPos = -15 + topMargin + (count * printFont.GetHeight(e.Graphics))
    41.             ' Prints the line using Graphics object
    42.             If Not row.IsNewRow Then
    43.                 e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 5, yPos)
    44.                 total += price
    45.             End If
    46.             count += 1
    47.         Next
    48.         yPos += 2
    49.         e.Graphics.DrawString("                      ________", printFont, System.Drawing.Brushes.Black, 5, yPos)
    50.         Dim Sum_amount As Integer = 0
    51.         Sum_amount = Len(tbox_importe.Text)
    52.         line_Total = StrDup(23 - Sum_amount, ".")
    53.         yPos += 12
    54.         e.Graphics.DrawString("Total " & line_Total & tbox_importe.Text, printFont, System.Drawing.Brushes.Black, 5, yPos)
    55.  
    56.         yPos += 2
    57.         e.Graphics.DrawString("     ¡Thank you for your", printFont, Brushes.Black, 5, yPos + 20)
    58.         e.Graphics.DrawString("            visit!", printFont, Brushes.Black, 5, yPos + 30)
    59.         e.HasMorePages = False
    60.  
    61.         Catch ex As Exception
    62.            MsgBox(ex.Message)
    63.         End Try
    64.     End Sub

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  14. #14

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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
  •  



Click Here to Expand Forum to Full Width