How to Print dgv which have multiple number of pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahul2310
    New Member
    • Oct 2013
    • 62

    How to Print dgv which have multiple number of pages

    I am new to vb.net windows form
    I have code (bit map method) using which i can print data grid but only one page not if grid has multiple pages(many rows)
    i want to print all the rows around 90 +
    Please help me with code to print dgv

    Code i am using now is below
    Code:
    Private Sub btnprint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprint.Click
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
            PrintPreviewDialog1.ShowDialog()
        End Sub
    
    
        Private Sub PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim bm As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
            DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))
            e.Graphics.DrawImage(bm, 0, 0)
        End Sub
  • rahul2310
    New Member
    • Oct 2013
    • 62

    #2
    i found it by googling around if some one gets stucked hole my answer helps,do following
    •First add Print Document1 and Print Preview Dialog1 to form on which data grid is placed from tool box.
    •Then select print document1, go to properties and go to print page event
    •In print page event write following code.
    Code:
    With DataGridView1
    Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
    fmt.LineAlignment = StringAlignment.Center
    fmt.Trimming = StringTrimming.EllipsisCharacter
    Dim y As Single = e.MarginBounds.Top
    Do While mRow < .RowCount
    Dim row As DataGridViewRow = .Rows(mRow)
    Dim x As Single = e.MarginBounds.Left
    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
    e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
    Else
    e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .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 Then
    e.HasMorePages = True
    mRow -= 1
    newpage = True
    Exit Sub
    End If
    Loop
    mRow = 0
    End With
    •On Click of print button write following code.
    Code:
    PrintPreviewDialog1.Document = print
    PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
    PrintPreviewDialog1.ShowDialog()
    Bye hope it helps

    Comment

    Working...