-
Jul 27th, 2015, 08:34 AM
#1
Thread Starter
Addicted Member
Problem printing using PrintDocument
I thought I had this working a few weeks ago as several posters helped me out. But I discovered it was not working. I populate a DataGridview from my SQL database. I then allow the user to click a print button. I then want to print a packing slip for each row in the DGV control. But the first row prints as many times as rows in my DGV control. So if I have 10 rows then the first packing slip prints 10 times while the other orders print once like I want it to.
I am a web developer who was thrown into this windows form project and it's been 20 years since I'v done this. I'm really weak on printing this way from windows forms. Here is my code:
Code:
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim csvFile As String = "H:\ShippingLabel.csv"
Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
Dim oOrder As New Order(connStringNW)
' Create columns in the DataTable.
dtChecked.Columns.Add("Id", GetType(Integer))
dtChecked.Columns.Add("OrderDate", GetType(DateTime))
dtChecked.Columns.Add("ShipToName", GetType(String))
dtChecked.Columns.Add("ShipAdd1", GetType(String))
dtChecked.Columns.Add("ShipCity", GetType(String))
dtChecked.Columns.Add("ShipState", GetType(String))
dtChecked.Columns.Add("ShipZip", GetType(String))
' Loop thru all orders
For Each band As DataGridViewBand In dgvOrder.Rows
iID = CInt(dgvOrder.Rows(band.Index).Cells(0).Value)
OrderDate = CDate(dgvOrder.Rows(band.Index).Cells(1).Value).ToShortDateString
ShipToName = CStr(dgvOrder.Rows(band.Index).Cells(2).Value)
ShipToAdd1 = CStr(dgvOrder.Rows(band.Index).Cells(3).Value)
ShipToCSZ = CStr(dgvOrder.Rows(band.Index).Cells(5).Value) & ", " & CStr(dgvOrder.Rows(band.Index).Cells(6).Value) & " " & CStr(dgvOrder.Rows(band.Index).Cells(7).Value)
Dim oOrderItem As New OrderItem(connStringNW)
Dim ds As New DataSet()
ds = oOrderItem.GetOrderItemsByOrderId(iID)
' Loop thru all items on order
For Each dr As DataRow In ds.Tables(0).Rows
If HoldCode = "3" Then
lItem.Add(CStr(dr("Sku")))
lDesc.Add(CStr(dr("Description")))
lQty.Add(CStr(dr("Quantity")))
iTotalQty += CInt(dr("Quantity"))
sngTotalWeight += CSng(dr("Weight")) * CInt(dr("Quantity"))
End If
Next
ds = Nothing
lItem.Clear()
lDesc.Clear()
lQty.Clear()
lWgt.Clear()
iTotalQty = 0
' Select the printer
PrintDocument1.PrinterSettings.PrinterName = cboPrinter.Text
PrintDocument1.PrinterSettings.Copies = 1
PrintDocument1.Print()
Next
outFile.Close()
dgvOrder.Columns.Clear()
btnExit.Visible = True
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'------------------------------------------------------
' Set Image Files
'------------------------------------------------------
Dim img1 As Image = Image.FromFile(Application.StartupPath + "\logo_left.png")
iLine = 460
'------------------------------------------------------
' Set Fonts
'------------------------------------------------------
printFont18B = New Font("Tahoma", 18)
printFont11 = New Font("Tahoma", 11)
'------------------------------------------------------
' Create Rectangles and Boxes
'------------------------------------------------------
Dim R12 As Rectangle
R12 = New Rectangle(560, 50, 120, 35) ' Date Heading
Dim R13 As Rectangle
R13 = New Rectangle(680, 50, 120, 35) ' Packing Slip Heading
Dim R14 As Rectangle
R14 = New Rectangle(560, 85, 120, 35) ' Date
Dim R15 As Rectangle
R15 = New Rectangle(680, 85, 120, 35) ' Order Id
Dim R16 As Rectangle
R16 = New Rectangle(30, 420, 130, 30) ' Item Code Heading
'Print Ship To
Dim R As Rectangle
R = New Rectangle(30, 225, 250, 130)
Dim bp1 As New Pen(Color.Black, 1)
Dim p1 As New Point(30, 248)
Dim p2 As New Point(280, 248)
e.Graphics.DrawRectangle(Pens.Black, R)
e.Graphics.DrawString("Ship To", printFont11, Brushes.Black, 35, 230)
e.Graphics.DrawLine(bp1, p1, p2)
e.Graphics.DrawString(ShipToName + vbCrLf, printFont11, Brushes.Black, 35, 250)
e.Graphics.DrawString(ShipToAdd1 + vbCrLf, printFont11, Brushes.Black, 35, 270)
e.Graphics.DrawString(ShipToCSZ + vbCrLf, printFont11, Brushes.Black, 35, 290)
' Print Top Header Packing Slip Info
e.Graphics.DrawString("Packing Slip", printFont18B, Brushes.Black, 590, 12)
e.Graphics.DrawRectangle(Pens.Black, R12)
e.Graphics.DrawString("Date", printFont11, Brushes.Black, 562, 60)
e.Graphics.DrawRectangle(Pens.Black, R13)
e.Graphics.DrawString("Order#", printFont11, Brushes.Black, 682, 60)
e.Graphics.DrawRectangle(Pens.Black, R14)
e.Graphics.DrawString(OrderDate, printFont11, Brushes.Black, 562, 90)
e.Graphics.DrawRectangle(Pens.Black, R15)
e.Graphics.DrawString(iID, printFont11, Brushes.Black, 682, 90)
If iOrderCount = dt.Rows.Count Then
e.HasMorePages = False
Return
Else
e.HasMorePages = True
End If
iOrderCount += 1
End Sub
Can anyone help?
-
Jul 27th, 2015, 08:57 AM
#2
Re: Problem printing using PrintDocument
I'd suggest you take a look at the code in this class...basically you don't need to call the PrintDocument.Print() inside a loop - you need the code in the printpage event handler to take care of printing one page worth of records and updating the "current record" index.
-
Jul 27th, 2015, 09:22 AM
#3
Thread Starter
Addicted Member
Re: Problem printing using PrintDocument
The article in your link is from 2006. It looks like it's referring at a DataGrid control while I'm using the DataGridView control. Maybe they are very close but it's been 20 years since I did any windows development so I'm not sure.
You said to move the PrintDocument.Print() outside the loop. So do I simply do this?
Next
PrintDocument.Print()
outFile.Close()
-
Jul 27th, 2015, 09:38 AM
#4
Re: Problem printing using PrintDocument
Actually I should have referred you to this article first
Yes - you call PrintDocument.Print() and put the workings of printing the current page into your PrintDocument_PrintPage() event handler. In that event handler you also decide if you have printed the whole document yet and - if not - set e.HasMorePages to true. This will call the event handler again to do the next page.
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
|