DataGrid print - please help

netnoon

New member
Joined
Feb 15, 2006
Messages
3
Programming Experience
Beginner
Hi all, please help. I am noob to .net programming and can´t solve the problem of datagrid print.

I use this simple code to print the datagrid control but do not know how to print more then one page. I have tried to set the property HasMorePages = true based on height od the grid but all I have achieved was to print the first page multiple times.

Could you please advise or send hyperlink where I can find something helpful ?

Private Sub PrintGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tisk_button.Click
printdocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printdocument1.PrintPage

Dim myPaintArgs As New PaintEventArgs(e.Graphics, New Rectangle(New Point(0, 0), Me.Size))
Me.InvokePaint(datagrid, myPaintArgs)
End Sub
 
Pretty ingenious print method !! - but it will only print what is visible on screen for that control.
 
If you create a UserControl and inherit it from DataGrid it is possible to add a ScrollToRow method. Combine this and your print will get more pages. I will not match exactly if half a row is visible but close. I tested this by scrolling
DataGrid1.VisibleRowCount and printing another page.

From Windows Forms FAQ I found this code to add to a custom datagrid control (http://www.syncfusion.com/FAQ/WindowsForms/Default.aspx#44 current index 5.60)
VB.NET:
Sub ScrollToRow(ByVal row As Integer) 
  If Not Me.DataSource Is Nothing Then 
    Me.GridVScrolled(Me, New ScrollEventArgs(ScrollEventType.LargeIncrement, row)) 
  End If 
End Sub
 
Re:

Thanks a lot! I used the code and can print the whole datagrid control divided into more printdocuments.

However there is a small bug that complicates this. As you mentioned this one prints only the visible rows of datagrid.

As I use the .VisibleRowsCount I am not able to fill the whole page and match the problem of unused space on the printed paper.

I tried to avoid this by enlarging size of the datagrid based on the current rowcount and height of the font.

But as I make the size of the grid bigger then of the form the grid can not be scrolled and I print the same first page more times.

Could you please advise ?
 
Last edited:
Well, this is a hack and not the way you're supposed to print the datagrid, I would not put too much effort into perfecting this method, though I'm sure it is possible to get nice multipage prints if you keep on tweaking it. One tip would be, if there is only 3 lines left after you have printed some full pages, then you must resize it to only display 3 gridlines and scroll to the third last row.
 
Don Delegate, that example is about regular printing methods (and in C#), this is not as I get it what this thread is about. netnoon is here utilizing the Frameworks own painting mechanism for printing by the Control.InvokePaint method. Of course this is very limiting compared to the dynamics of regular printing methods, but it also is very easily implemented for those cases that fit the bill.

netnoon, my "visible on screen" was wrong, "visible on control" is it, the control can be moved out of visible form too, which is perfect for this kind of thing with resizing the control between the pages.
 
Back
Top