-1

This code is printing all the rows in a DataGridView, printing them on multiple pages if need be.

I made sure to use e.HasMorePages properly, and even stepped through the code, and it is properly exiting the PrintPage routine at the end of page 1, so that page 2 can be printed. But when it finishes, both pages are overlaid on top of each other instead of on their own pages.

Code:

' These are all at the beginning of the class (form)

Private WithEvents printDocument1 As New Printing.PrintDocument

Private ColumnCount As Integer = 0
Private RowCount As Integer = 0
Private CurrRow As Integer = 0

Private CellTopPos As Integer = 0
Private CellLeftPos As Integer = 0

' this is the button on the form that runs the print routine
Private Sub btnPrint_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnPrint.Click

    CurrRow = 0
    PrintDocument1.Print()

End Sub

Private Sub printDocument1_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage

    ' start printing current row at top of page
    With printDocument1

        CellTopPos = .DefaultPageSettings.Margins.Top
        While CellTopPos < .DefaultPageSettings.PaperSize.Width - _
            .DefaultPageSettings.Margins.Bottom

            ' start printing current row at left of page
            CellLeftPos = .DefaultPageSettings.Margins.Left
            For Cell = 0 To ColumnCount - 1

                Dim CellValue As String = _
                    grdSearch.Rows(CurrRow).Cells(Cell).Value.ToString()
                Dim CellWidth = _
                    grdSearch.Rows(CurrRow).Cells(Cell).Size.Width + 50
                Dim CellHeight = _
                    grdSearch.Rows(CurrRow).Cells(Cell).Size.Height

                Dim Brush As New SolidBrush(Color.Black)
                e.Graphics.DrawString(CellValue, _
                    New Font("Century Gothic", 10), Brush, CellLeftPos, CellTopPos)
                e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, _
                    CellTopPos, CellWidth, CellHeight)

                CellLeftPos += CellWidth
            Next

            CellTopPos += grdSearch.Rows(CurrRow).Cells(0).Size.Height
            CurrRow = CurrRow + 1
            If CurrRow = RowCount Then Exit While

        End While

    End With

    If CurrRow < RowCount Then
        e.HasMorePages = True
    Else
        e.HasMorePages = False
    End If

End Sub

Hasty edit: ColumnCount and RowCount are calculated when the the dataGridView is filled in the FormLoad sub

  • Shouldn't your While loop be using `.DefaultPageSettings.PaperSize.Top` instead of `.DefaultPageSettings.PaperSize.Width`? It seems that `Width` will always be smaller than `Bottom`, resulting in a negative number (I'm assuming a standard Letter sized paper printed in portrait orientation). If seems to me you don't need to subtract `Bottom` from `Top`, just compare `CellTopPos` to `Bottom`. – Chris Dunaway Jan 11 '16 at 21:12
  • Off-Topic but the while should be this way:`While CellTopPos < .DefaultPageSettings.PaperSize.Height - .DefaultPageSettings.Margins.Bottom1` and by the way, it worked properly when I used `grdSearch.RowCount` and `grdSearch.ColumnCount` – Reza Aghaei Jan 11 '16 at 21:24
  • Chris: True, it's not clear, but I'm using landscape, so that's why I was using width. Alas, whether in portrait or landscape, it still runs to next page, and thus the issue. As for why I used landscape, it's because I know the width of the columns in the grid, and knew it was too big for portrait And it's not that I don't know how to print multi-page either, which is the frustrating part. I have a multi-page print routine in another part of code and it works just fine. It's printing to two sides of a card (like an ID badge) – kentechsoftware Jan 12 '16 at 22:53

1 Answers1

0

Alright, I can't say I understand why, but I got it working. Like I said, I have another routine that works, and it's in a Module, while this current routine (that doesn't work properly), is in a Form. I transplanted the non-working form print routine over into the module, and it suddenly started working properly. Both are code-created, so again, no idea why the difference, but hey. It works.