Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

If I use printpreviewdialog1.showdialog(), it works perfect. But if I use, it shows empty.

Is anyone having the same issue or have an idea why it happens?

Thank you.

What I have tried:

Updated 9-Aug-23 7:43am

The reason why the 'printpreviewdialog1' shows up is because it is the named control derived from the actual control 'printpreviewdialog' Class - see full explanation and sample code - MS Learn | PrintPreviewDialog Class[^]

The object will be declared as such -
//Declare the dialog...
internal PrintPreviewDialog PrintPreviewDialog1;

The 'PrintPreviewDialog.ShowDialog()' method displays the print preview dialog as a modal dialog box. When you call 'ShowDialog()', it will block the execution of code until the dialog is closed by the user.

The 'PrintPreviewDialog.Show()' method displays the print preview dialog as a non-modal dialog box. When you call 'Show()', it will not block the execution of code, and your app continues to run while the print preview is open meaning that the code after the 'Show()' method will continue to execute without waiting for the user to close the preview.

When your 'PrintPreviewDialog.Show()' metod is showing an empty preview, it's possible that the code following the 'Show()' method might be modifying or refreshing the content of the print preview, causing it to appear empty.

Use the link above to use the proper methods and you should be fine.

Referring to the 'DataGridViewPrinter' Class, it seems you are making use of a created control done by one of our members Salan Alani. Read through his documentation properly as I think his methods might differ from what you are trying to do using a DataGrid - CodeProject Articles | The DataGridViewPrinter Class[^]
Share this answer
FcoJimenez 3-Aug-23 9:03am    
Thank you Andre. It is a great information about blocking the execution of the code. Must be something in the PRINTPAGE event of the PRINTDOCUMENT or in the PUBLIC SUB PRINTPREVIEW (). I will check it closer and when I found the issue will comment it here.
Andre Oosthuizen 3-Aug-23 9:58am    
You're welcome.
We have no idea what the "DATAGRIDVIEWPRINTER" class is, or how it works: it is not a standard .NET class and we have no idea where you got it from, or how it works.
Moreover, we have no idea how you ar eusing is, since we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with; we get no other context for your project.

So go back to where you got the class from and ask them: we can't help you!
Share this answer
Andre Oosthuizen 2-Aug-23 7:53am    
Looks like a class created by a CP member, see my link below.
Thanks OriginalGriff and Andre for your help. I found the solution. It was something wrong in the document.printpage. I am working on a Cattle Managment Software, with a tab control.

tabpage 0 :
Home Page

tabpage 1 :
Cow Information (2 DatagridViews)

tabpage 2 :
Reports (2 DatagridViews)

tabpage 3 :
PrintPreview (1 printpreviewcontrol)

Inside the printpage event of the document I had :

Select case tabcontrol.selectedindex

case 1
"prints the cow information"

Case 2
"Prints the Report"

end select

tabcontrol.selectedindex = 3

So when runing it changed the tabpage before generating pages, and printpreviewcontrol showed empty.

I fixed it creating a varible integer


When tabpage 1 is focused PFORM = 1
tabpage 2 is focused PFORM = 2

document Printpage event

Select case PFORM

case 1
"prints the cow information"

Case 2 
"Prints the Report"

end select

tabcontrol.selectedindex = 3

Thank You Very much.
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