Question

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) :/

Basic example of code:

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

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top