Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!!

I am using the DataGridView Printer Class and trying both PrintPreviewDialog and PrintPreviewControl. PrintPreviewDialog works perfect but PrintPreviewControl is not showing the document. Thank you in advance.

What I have tried:

VB
Private Sub PRINTPREVIEW()
        CPagesettings()
        Dim DocName As String
        DocName = GetInteger("SELECT DISTINCT(NAME) FROM RANCH")
        REPORTDATE = Today
        REPORTNAME = "PRUEBA"
        Try
            Select Case TabControlMAIN.SelectedIndex
                Case 1
                    MyPrintDocument.DocumentName = (DocName & vbCrLf & (CDate(REPORTDATE)) + "  " + TimeOfDay & vbCrLf & REPORTNAME)

                    DGVPRINT = New DGVPRINTER(DGVREPORT, MyPrintDocument, False, True, MyPrintDocument.DocumentName,
                                                                            New Font(DGVREPORT.DefaultCellStyle.Font, FontStyle.Regular),
                                                                            Color.Black, True)
                    MyPrintPreviewControl.Document = MyPrintDocument

                Case 3
                    MyPrintDocument.DefaultPageSettings.Landscape = True
                    MyPrintPreviewControl.Document = MyPrintDocument
            End Select

        Catch ex As Exception
        End Try

        MyPrintPreviewControl.Document = MyPrintDocument
        TabControlMAIN.SelectedTab = TabPagePREVIEW
End Sub

Private Sub MyPrintDocument_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles _
        MyPrintDocument.PrintPage

        Select Case TabControlMAIN.SelectedIndex
            Case 1
                Try
                    Dim more As Boolean = DGVPRINT.DrawDataGridView(e.Graphics)
                    If more = True Then
                        e.HasMorePages = True
                    End If

                    MyPrintPreviewControl.Rows = 1

                    ToolStripStatusLabel3.Visible = True
                    If DGVPRINTER.PageNumber > 1 Then
                        PrevText = DGVPRINTER.PageNumber & " Pages"
                        ToolStripStatusLabel3.Text = PrevText
                    Else
                        PrevText = MyPrintPreviewControl.Rows & " Page"
                        ToolStripStatusLabel3.Text = PrevText
                    End If

                Catch ex As Exception
                End Try
Posted
Updated 2-Jul-23 18:23pm
v2
Comments
Dave Kreskowiak 2-Jul-23 15:33pm    
If you're talking about a control from an article, ask your question in the forum at the bottom of the article. Articles are supported by the people that wrote them, not the community at large.
Member 16041854 2-Jul-23 22:18pm    
Thank you Dave. It is weird that is not showing any error but preview is empty. I will try both, break points and forums.

Quote:
Printpreviewcontrol not showing document. No errors.
Start by not just swallowing exceptions:
VB
Try
    Select Case TabControlMAIN.SelectedIndex
    ...
    End Select

Catch ex As Exception
End Try
If you get an error in that code, you throw it away and never know it occurred.
At a minimum, log the error to the debug console or a log file so that you can investigate what it was and why it occurred.

When you specifically discard any exceptions, you will get "No errors" - but stuff still won't work if there is a problem only you will have no idea why!
 
Share this answer
 
Comments
FcoJimenez 3-Jul-23 8:35am    
Thank you OriginalGriff. I will try that.
OriginalGriff 3-Jul-23 9:11am    
You're welcome!
FcoJimenez 5-Jul-23 13:17pm    
Hi OriginalGriff

I removed the try catch lines. And still no erros and blank page. It is frustating because I had working before printing the datagridview from one tabpage, but when I tried to include another DGV from another tabpage. Depending on which tabpage is selected. The problem started. It is weird because PrinPreviedialog shows the content of DGV but PrintPreviewControl does not. I keep trying and post the solution.
Thank you.
I am not familiar with this control, so I can't directly help solve your problem, there is not enough information here for me to work with. "... not Showing ..." tells me very little. Have you set a breakpoint and stepped through your code to check against the documentation, to see that it is doing what it should?

Here is some sound advice for you:

Generally, when using controls or libraries that you are not familiar with, it is a good practice to create a small new project, add dummy data, and test the control or library. Use debugging techniques. Set breakpoints, step through the code, check what it is doing.

Read the documentation, ask questions in their support forums, github/repo, blog posts, articles, etc. If they do not answer, then maybe, it is not a good control or library to use. To me, that would be a BIG red flag not to use it.

If the support is there, you understand how it works, and your test project works, then add it to your main project.
 
Share this answer
 

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