Print a long datagridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GFVB
    New Member
    • Jun 2014
    • 3

    #1

    Print a long datagridview

    HI,
    I WORK WITH PRINTDOCUMENT CONTROL AND I WANT TO PRINT MORE THAN 1 PAGES. I PUT IN PrintDocument1_ PrintPage CODE :

    Code:
     Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
            Dim Lm As Integer
            Dim Rm, dgvRows As Integer
            Dim HasMorePages As Boolean = True
            Static pagenum As Integer
            With Form9.dgv2
                Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
                fmt.LineAlignment = StringAlignment.Center
                fmt.Trimming = StringTrimming.EllipsisCharacter
                Dim y As Single = e.MarginBounds.Top  'ΕΧΕΙ ΤΙΜΗ 100
                newPage = True
                Do While mRow < Form9.dgv2.RowCount                 
                    Dim row As DataGridViewRow = Form9.dgv2.Rows(mRow)
                    Dim x As Single = e.MarginBounds.Left - 95
                    Dim h As Single = 0
                      
                    For Each cell As DataGridViewCell In row.Cells
                        Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                        e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                        If (newPage) Then
                            PrintFont = New Font("arial black", 7)
                            e.Graphics.DrawString(Form9.dgv2.Columns(cell.ColumnIndex).HeaderText, PrintFont, Brushes.Black, rc, fmt)
                        Else                      e.Graphics.DrawString(Form9.dgv2.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), Form9.dgv2.Font, Brushes.Black, rc, fmt)
                        End If
                        x += rc.Width
                        h = Math.Max(h, rc.Height)
                    Next
                    newPage = False
                    y += h
                    mRow += 1
                    If y + h > e.MarginBounds.Bottom - 100 Then
                        e.HasMorePages = HasMorePages
                        mRow = mRow - 1                     
                        Exit Sub
                    End If
                Loop
                mRow = 0
                
            End With
    
        End Sub ]
    
    IN CREATING PREVIEWS MESSAGEBOX SHOWS THAT CREATES 2 PAGES. IN THE PRINDOCUMET AREA IN THE SCREEN SHOWS THE 1 PAGE. IN PRINT BUTTON PUT THE CODE :
    [ Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            PrintPreviewDialog1.Document = PrintDocument1 
            PrintPreviewDialog1.ShowDialog()  
        End Sub
    BUT WHEN I PUSH PRINT BUTTON PRINTS ME ONLY THE SECOND PAGE.
    CAN ANYONE HELP ME ?
    THANKS
    Last edited by Rabbit; Jun 12 '14, 03:08 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Jennypliskin
    New Member
    • Sep 2013
    • 30

    #2
    There isn't any built in printing support, so there are two options:
    1) Use the standard printing system provided by Windows Forms and manually print out the information that you want. Check out the documentation for PrintPage and PrintDocument

    2) Use the DataGridView's clipboard copy support to copy the content to an Excel file and then print that.

    Comment

    • GFVB
      New Member
      • Jun 2014
      • 3

      #3
      Jennypliskin,
      Thanks a lot for your help.

      Comment

      Working...