anything for printing datagridviews?

sabe

Member
Joined
Aug 8, 2006
Messages
17
Programming Experience
Beginner
have you got anything good for printing, esp datagridviews?
 
Last edited by a moderator:
That does work, but do you know any nicer way - maybe to print just the Datagridview direct, not as a bitmap (lots of ink), really sad but I do have to consider 1300 students using all my ink!! lol!
 
My bad, it's one i'd heard about. Never thought to consider it wasnt part of the framework. Never the less, Here's a usfeul bit of code i found that will do what you want. See the attached file...
 

Attachments

  • PrintDataGridView.zip
    1.8 KB · Views: 104
From what i can see you'll need to drag a print document onto your from and call it 'PrintDocument1' Then when you want to print ..

Me.PrintDocument1.Print
 
Some posts splitted out to new thread here as 'textbox arrays' and 'datagridview printing' isn't much related topics.
 
ahh the mists clear, and that seems to work (for some reason in the email it had Me.PrintDocument1.BeginPrint not me.printdocument1.print).

Cheers though it works v nice, cell height does not seem to be right but I am sure a few hours and I should have it !! lol!
 
ahh sorted the line :

nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11

changed to:

nHeight = e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 30
 
code exception

I have implemented the code in the attached file and ran into a problem that I cant seem to fix. In the code under Sub PrintDocument1_PrintPage
I continue to get an exception in the section

VB.NET:
For Each oCell As DataGridViewCell In oRow.Cells
 
If oColumnTypes(i) Is GetType(DataGridViewTextBoxColumn) OrElse oColumnTypes(i) Is GetType(DataGridViewLinkColumn) Then
Try
e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font, New SolidBrush(oCell.InheritedStyle.ForeColor), _
    New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)
 
Catch ex As NullReferenceException
MessageBox.Show("we had a null reference exception")
End Try
 
 
ElseIf oColumnTypes(i) Is GetType(DataGridViewButtonColumn) Then
I used a try catch to catch the nullreference exception but I do not understand what is causing this exception. I have made sure that all cells in the datagridview have some value, but this didnt fix the problem either.
Any help would be greatly appreciated.Thanks in advance.
-Erik-
 
Last edited by a moderator:
I have run into a problem with this as well. But the only problem I have is that it wants to print the entire datagrid, which for me is over 900 rows, when I call the print method. I have passed the printdocument to a printdialogue and am calling the print method from there. I thought this would allow me to specify which pages to print, etc... I set it to AllowSomePages etc... and yet it still sends the entire job to the printer. I will probably trial and error my way through in the next hour or so (hopefully) but any help would be appreciated. Thx.
 
Back
Top