Results 1 to 2 of 2

Thread: printing data grid

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    5

    printing data grid

    how do i create a print preview for a datagrid control currently displayed, and later print it out in a customized report format? any help would be appreciated. thanks

  2. #2
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256
    http://www.c-sharpcorner.com/Graphic...dPrinterMG.asp

    i am currently working on doing just that, but havent had any luck yet. anyone that could help... the code needs to be transferred to VB.net. This would be a powerful tool, because printing xml files are unreadable to a user, so if you can print the data like a datagrid, it would solve the problem. anyway, here is code that will print the raw data...

    VB Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.         Dim LinesPerPage As Single = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics)
    3.         Dim Line As String = Nothing, count As Integer, yPos As Single = 0
    4.         For count = 0 To LinesPerPage - 1
    5.             Line = StreamToPrint.ReadLine
    6.             If Line Is Nothing Then Exit For
    7.             yPos = e.MarginBounds.Top + count * PrintFont.GetHeight(e.Graphics)
    8.             e.Graphics.DrawString(Line, PrintFont, Brushes.Black, e.MarginBounds.Left, yPos, New StringFormat())
    9.         Next
    10.         If Not (Line Is Nothing) Then e.HasMorePages = True
    11.     End Sub
    12.  
    13.     Private Sub MenuItemPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemPrintPreview.Click
    14.         StreamToPrint = New IO.StreamReader("temp.xml")  'whatever is bound to datagrid
    15.         PrintPreviewDialog1.Document = PrintDocument1
    16.         PrintPreviewDialog1.ShowDialog()
    17.         If Not (StreamToPrint Is Nothing) Then
    18.             StreamToPrint.Close()
    19.         End If
    20.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width