Click here to Skip to main content
15,886,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a DataGridView that contains data and images, fetched from database. I have to print this data grid view.
Problem:
1. DataGridView contains data that will not fit in one page, so have to print in multiple pages.
2. The Images are not appearing in print preview.
3. Data that exceeds first page doesn't appear in next page or overwritten in the first page(print preview).
The Code I have tried prints data in the first page and in the next page there are only column headers(no rows).

*My code opens the print preview dialog first and then I have to print it.
** I have also modified the Print option(ToolStrip) of the PrintPreviewDialog..

What I have tried:

VB
Private Sub Print_lst_Click(sender As Object, e As EventArgs) Handles Print_lst.Click
        Dim b As New ToolStripButton
        b.Image = CType(PrintPreviewDialog1.Controls(1), ToolStrip).ImageList.Images(0)
        b.ToolTipText = "Print"
        b.DisplayStyle = ToolStripItemDisplayStyle.Image
        AddHandler b.Click, AddressOf PrintPreview_PrintClick
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.RemoveAt(0)
        CType(PrintPreviewDialog1.Controls(1), ToolStrip).Items.Insert(0, b)
        AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
        ' PrintPreviewDialog1.StartPosition = FormStartPosition.CenterParent
        ' PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.Document = PrintDocument1
        PrintDocument1.OriginAtMargins = True
        PrintDocument1.DefaultPageSettings.Margins = New Drawing.Printing.Margins(0, 0, 0, 0)
        'PrintPreviewDialog1.Icon = Icon
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintPreview_PrintClick(sender As Object, e As EventArgs)
        Try
            PrintDialog1.Document = PrintDocument1
            If PrintDialog1.ShowDialog() = DialogResult.OK Then
                PrintDocument1.Print()
            End If
        Catch ex As Exception
        End Try
        clean()
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim x As Integer = 50
        Dim y As Integer = 50
        Dim header As Boolean = True
        'draw headers
        Dim j As Integer = 0
        Do While (j < Me.DataGridView1.Columns.Count)
            Dim rect As Rectangle = New Rectangle(x, y, Me.DataGridView1.Columns(j).Width, Me.DataGridView1.ColumnHeadersHeight)
            e.Graphics.FillRectangle(Brushes.LightGray, rect)
            e.Graphics.DrawRectangle(Pens.Black, rect)
            If (Not (Me.DataGridView1.Columns(j).HeaderText) Is Nothing) Then
                e.Graphics.DrawString(Me.DataGridView1.Columns(j).HeaderText, SystemFonts.DefaultFont, Brushes.Black, rect)
            End If
            x = (x + rect.Width)
            j = (j + 1)
        Loop
        x = 50
        y = (y + Me.DataGridView1.ColumnHeadersHeight)
        'draw rows
        For whRow = whRow To Me.DataGridView1.RowCount - 1
            Dim drow As DataGridViewRow = Me.DataGridView1.Rows(whRow)
            j = 0
            Do While (j < Me.DataGridView1.Columns.Count)
                Dim cell As DataGridViewCell
                cell = drow.Cells(j)
                Dim rect As Rectangle = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)

                e.Graphics.DrawRectangle(Pens.Black, rect)
                If (Not (cell.Value) Is Nothing) Then
                    e.Graphics.DrawString(cell.Value.ToString, SystemFonts.DefaultFont, Brushes.Black, rect)

                End If
                x = (x + rect.Width)
                j = (j + 1)
            Loop
            x = 50
            y = (y + drow.Height)
            '----------------------New page----------------------------
            If (y > e.MarginBounds.Bottom) Then       'Print new page
                e.HasMorePages = True
'if I add y=50 or Exit Sub here then the remaining data(that doesn't fit in the firstPage is overwritten in the firstpage (2nd page contains the header only)
            End If
            '-----------------------------------------------------------------
        Next
    End Sub
Posted
Updated 7-Sep-18 19:29pm

1 solution

You don't "bend" an existing view that suits it's purpose to suit something else.

Create a new view (derived from the "online" view) that will "fit" the page. Run it with the same data source as the "online version".

Usually just a matter of "stretching" dimensions and / or controlling page breaks.
 
Share this answer
 
Comments
Debsbond008 8-Sep-18 2:37am    
whats the error in this code? be specific

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900