使用printdocument打印多页 [英] Printing multiple pages with printdocument

查看:146
本文介绍了使用printdocument打印多页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写程序,其中:在datagridview中选择了几行后,它会检查是否选择了1个或更多行.如果是一份,则在一页上打印两份报告(一份=半页).如果更多:每页打印两份报告,打印所需的页数.问题是,我的代码打印了相同报告的4531456453页(第一行和第二行):/

I'm making program, which: After you select few rows in datagridview it checks if 1 or more was selected. If one, print two copies of report on one page(One report = half page) If more: Print two reports per page, printing as much as needed pages. Problem is, my code prints 4531456453 pages of same report(first and second row) :/

基本代码示例:

yPos = 0
Do While tmpI < mydatagridview.SelectedRows.Count - 1
For Each selectedrow As DataGridViewRow In mydatagridview.SelectedRows
    Dim data as string = mydatagridview.SelectedRows(selectedrow.index).cells(1).value
    Dim data2 as string = mydatagridview.SelectedRows(selectedrow.index).cells(12).value

    e.graphics.drawstring(data, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data, drawfont).width/2), 25+yPos)
    e.graphics.drawstring(data2, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data2, drawfont).width/2), 50+yPos)
    yPos += e.pagebounds.height/2
    tmpI += 1 
    If yPos > e.pagebound.height/2 Then
        h = 0
        e.HasMorePages = true
        Exit Sub
    End If
Next selecedrow
Loop

截至目前,就像我之前说过的那样,它从带有索引0和1的SelectedRows中打印出无限量的包含数据和data2的页面.

As of right now, as I said before it prints infinite amount of pages having data and data2 from SelectedRows with indexes 0 and 1.

推荐答案

希望这会帮助......

Hope this helps ...............

Sub PrintIt(ByVal e As System.Drawing.Printing.PrintPageEventArgs, byval nRow as Integer,ByVal nY As Integer) 

    Dim data as string = mydatagridview.SelectedRows(nRow).cells(1).value
    Dim data2 as string = mydatagridview.SelectedRows(nRow).cells(12).value

    e.graphics.drawstring(data, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data, drawfont).width/2), 25+nY)
    e.graphics.drawstring(data2, drawfont, (e.graphics.pagebound.width/2-e.graphics.measurestring(data2, drawfont).width/2), 50+nY)

End Sub

在您的代码中还有一些修饰符..

And some modif in your code ..

yPos = 0

If mydatagridview.SelectedRows.Count = 1

    PrintIt(e,0,yPos)

    yPos += e.pagebounds.height/2

    PrintIt(e,0,yPos)

Elseif mydatagridview.SelectedRows.Count > 1

Dim x,n As Integer

    For x = 0 to mydatagridview.SelectedRows.Count-1
        If n = 2 Then
             e.HasMorePages = true
             n = 0
             yPos = 0
         End If 

         PrintIt(e,x,yPos)
         yPos += e.pagebounds.height/2

         n += 1
    Next

End If

这篇关于使用printdocument打印多页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆