从ODBC连接打印记录 - 被困在第一页

时间:2014-03-14 15:39:17

标签: vb.net

我正在从ODBC连接打印记录,但我无法打印超过第一页的内容。我下面的代码生成相同第一页的多个副本。如何迭代我的记录并在必要时仍然创建分页符?

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    'MsgBox("Printing functionality is still under construction.", MsgBoxStyle.Information)
    Dim RecordDoc As Drawing.Printing.PrintDocument
    RecordDoc = New Drawing.Printing.PrintDocument

    With RecordDoc.DefaultPageSettings
        .Landscape = False
        .Margins.Left = 50
        .Margins.Right = 50
        .Margins.Top = 50
        .Margins.Bottom = 50
    End With

    RecordDoc.DocumentName = "Print Records"
    AddHandler RecordDoc.PrintPage, AddressOf Me.printrecords

    dlgPreview.Document = RecordDoc
    dlgPreview.ShowDialog()
    RecordDoc.Dispose()
End Sub

Private Sub PrintRecords(ByVal sender As Object, ByVal e As Drawing.Printing.PrintPageEventArgs)

    Dim lvi As ListViewItem
    Dim Conn As OdbcConnection
    Dim Reader As OdbcDataReader
    Dim strFname As String = ""
    Dim strLname As String = ""
    Dim strReportHdr As String = ""
    Dim strMainCategory As String = ""
    Dim strColumnHeader As String = ""
    Dim intLinesPerPage As Integer = 0

    Dim x, y As Single
    Dim myfont As Font = New Font("Arial", 12, FontStyle.Regular)
    Dim myPen As New Pen(Color.Black, 3)


    'DEFINE THE REPORT HEADER BASED ON LISTVIEW SELECTIONS
    'FIRST, GET THE COLUMN NUMBER SELECTED. YOU'LL USE THIS FOR THE REPORT HEADER AS WELL AS WHEN PRINTING DATA
    Dim PrintColHeader As ColumnHeader = ListView1.Columns(intLViewColSort)

    'REMOVE THE < OR > AS NECESSARY, IF PRESENT
    If PrintColHeader.Text.StartsWith("> ") Then
        strColumnHeader = Mid(PrintColHeader.Text, 3) & ", in descending order"
    ElseIf PrintColHeader.Text.StartsWith("< ") Then
        strColumnHeader = Mid(PrintColHeader.Text, 3) & ", in ascending order"
    End If

    If rdoCustInfo.Checked = True Then
        strMainCategory = "Customer Information"
    ElseIf rdoCustVendPrefs.Checked = True Then
        strMainCategory = "Customer Vendor Preferences"
    ElseIf rdoPricePts.Checked = True Then
        strMainCategory = "Customer Price Points"
    ElseIf rdoSalesHist.Checked = True Then
        strMainCategory = "Customer Sales History"
    ElseIf rdoSpecific.Checked = True Then
        strMainCategory = "Other"
    Else
        MsgBox("System error with print function. Have a glass of wine.", MsgBoxStyle.Critical)
        Exit Sub
    End If

    y = e.MarginBounds.Y
    x = e.MarginBounds.X

    e.Graphics.DrawRectangle(myPen, e.MarginBounds.X, e.MarginBounds.Y + 20, 500, 1)
    e.Graphics.DrawString(strMainCategory & " - " & strColumnHeader, myfont, Brushes.Black, x, y)

    y += CInt(2 * myfont.GetHeight(e.Graphics))
    myfont = New Font("Arial", 10, FontStyle.Regular)

    intLinesPerPage = e.MarginBounds.Height / myfont.GetHeight(e.Graphics)

    'Open Connection TO ASC
    Conn = New OdbcConnection(ConnString)
    Conn.Open()

    For Each lvi In ListView1.Items

        'Execute Query
        cmdString = "select lastname,firstname,street1,street2,city,state,zipcode,phonenum,emailaddress from customer where customernum=" & lvi.Text

        Dim Cmd As New OdbcCommand(cmdString, Conn)
        Reader = Cmd.ExecuteReader()

        'Process The Result Set
        While (Reader.Read())
            Dim tempy As Integer
            tempy = y

            Dim CustName As String = Trim(Reader("firstname")) & " " & Trim(Reader("lastname"))
            CustName = StrConv(CustName, VbStrConv.ProperCase)
            e.Graphics.DrawString(CustName, myfont, Brushes.Black, e.MarginBounds.X, y)

            y += CInt(myfont.GetHeight(e.Graphics))
            Dim street As String = ""
            If Trim(Reader("street2").ToString) <> "" And Not (IsDBNull(Reader("street2"))) Then
                street += Trim(Reader("street1").ToString) & vbCrLf & Trim(Reader("street2"))
            Else
                street += Trim(Reader("street1").ToString)
            End If
            street = StrConv(street, VbStrConv.ProperCase)
            e.Graphics.DrawString(street, myfont, Brushes.Black, e.MarginBounds.X, y)

            y += CInt(myfont.GetHeight(e.Graphics))
            Dim CityStateZip As String = ""
            CityStateZip = StrConv(Trim(Reader("city")), VbStrConv.ProperCase) & ", " & Trim(Reader("state")) & ", " & Reader("zipcode")
            e.Graphics.DrawString(CityStateZip, myfont, Brushes.Black, e.MarginBounds.X, y)

            x += 200
            e.Graphics.DrawString(Trim(Reader("phonenum")), myfont, Brushes.Black, x, tempy)
            tempy += CInt(myfont.GetHeight(e.Graphics))
            y += CInt(2 * myfont.GetHeight(e.Graphics))
            e.Graphics.DrawString(Trim(Reader("emailaddress")), myfont, Brushes.Black, x, tempy)

            x = e.MarginBounds.X

            'If intPrintLineCount1 > intLinesPerPage Then
            '    e.HasMorePages = True
            '    intPrintLineCount1 = 0
            'Else
            '    e.HasMorePages = False
            'End If

            If y + myfont.Height > e.MarginBounds.Bottom Then
                e.HasMorePages = True
            End If

            'intPrintLineCount1 += 10

        End While
        Cmd.Dispose()
    Next
    Reader.Close()
    Conn.Close()
    Conn.Dispose()

End Sub

0 个答案:

没有答案