Help with "DataGridViewPrinter" class

timh

Well-known member
Joined
Nov 28, 2008
Messages
50
Programming Experience
Beginner
I've downloaded some VB.NET code for a DataGridViewPrinter class to use in my project, but I don't seem to be able to get it to work properly. Hopefully someone else is using this same code and can advise.

The code I am using is from this link...

CodeKeep Snippet : datagridview printer class (VB.NET)

As I read it, this code has functionality built in to print a DGV with columns spanning more than one page, however this doesn't seem to be happening for me, so I'm losing half my DGV. Anyone else encountered this problem? I'm sure it's something I'm doing (or not doing).

To summarise, a DGV that fits on one page is fine, but columns that run off the edge of the page are lost.

Thanks in advance for any suggestions.
 
That class seems a bit unfinished, but you're supposed to create and keep an instance of it in a class variable (private field), then in PrintPage event handler for your PrintDocument you call it like this:
e.HasMorePages = dgvprinter.DrawDataGridView(e.Graphics)
 
Thanks for the tip. It was all down to the way I was calling the class. i.e. wrong!

Seems to be working as it should now.
 
I've downloaded some VB.NET code for a DataGridViewPrinter class to use in my project, but I don't seem to be able to get it to work properly. Hopefully someone else is using this same code and can advise.

The code I am using is from this link...

CodeKeep Snippet : datagridview printer class (VB.NET)

As I read it, this code has functionality built in to print a DGV with columns spanning more than one page, however this doesn't seem to be happening for me, so I'm losing half my DGV. Anyone else encountered this problem? I'm sure it's something I'm doing (or not doing).

To summarise, a DGV that fits on one page is fine, but columns that run off the edge of the page are lost.

Thanks in advance for any suggestions.

it was a long time ago, you got the code working. How do i get the code working. i'm a real rookie and a beginner. So can you help my with the basics of this class? i've got a button and a DataGridView filled with database records. All is working fine but i try to print the DataGridView. How do i do that?

Thank you!
 
Looking back at my code in an effort to advise...

Let me see if I can explain what I did, without simply giving you the code.

My "Print" button calls a function called "SetupThePrinting", which returns a true or false value. If it returns false, the print process aborts. If it returns true, the print method of the PrintDocument is called.

VB.NET:
If SetupThePrinting() Then myPrintDocument.Print()

The SetupThePrinting function sets up the variables that you need to pass to the DataGridViewPrinter class (i.e. the DGV you want to print, the PrintDocument, center on page flag, with title flag, title text, title font, title color, page numbering flag).

You then bring up a print dialog and pass the settings the user selects to the PrintDocument. If you cancel out of the print dialog, the SetupThePrinting function returns "False" and the print process is aborted.

Otherwise, it's a matter of generating an instance of the DataGridViewPrinter class. (I think that's the right terminology, I'm a hobbyist remember!)

VB.NET:
myDataGridViewPrinter = New DataGridViewPrinter(myDGV, myPrintDocument, True, True, myPrintTitle, New Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)

You then exit the function, returning "True" and the print method of the PrintDocument object is called (as per the first code snippet).

If it makes things clearer, the code attached to the Print button could equally be written

VB.NET:
If SetupThePrinting = True Then myPrintDocument.Print()

Within the print method, you call the DrawDataGridView method of the DataGridViewPrinter object, as per the response from JohnH to my original query.

Hope that helps.
 
Back
Top