Results 1 to 6 of 6

Thread: Here's code to print, printpreview, & pagesetup for a datagrid

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    54

    Thumbs up Here's code to print, printpreview, & pagesetup for a datagrid

    Here is the code for all of those that would like to enable printPreview, PageSetup, and Print on a datagrid:

    Imports System.Drawing.Printing
    Private PrintPageSettings As New PageSettings()

    'For PrintPreview
    Private Sub PrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreview.Click


    Try
    PrintDocument1.DefaultPageSettings = PrintPageSettings
    PrintPreviewDialog1.Document = PrintDocument1
    PrintPreviewDialog1.ShowDialog()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub

    'For PageSetup

    Private Sub PageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetup.Click
    Try
    PageSetupDialog1.PageSettings = PrintPageSettings
    PageSetupDialog1.ShowDialog()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub

    'To Print
    Try

    PrintDocument1.DefaultPageSettings = PrintPageSettings
    PrintDialog1.Document = PrintDocument1
    Dim Result As DialogResult = PrintDialog1.ShowDialog
    If Result = DialogResult.OK Then
    PrintDocument1.Print()
    End If
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try

    Also put this code in the PrintDocument _PrintPage Event:


    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim myArgs As New PaintEventArgs(e.Graphics, New Rectangle(New Point(0, 0), Me.Size))
    Me.InvokePaint(DataGrid1, myArgs)

    End Sub


    Only one problem still remains : PRINTING MULTIPLE PAGES!
    Last edited by Nintendo_Wizard; Jan 7th, 2003 at 09:39 PM.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Is this what you want to print?
    StringToPrint = DataGrid1.Text

    If so, you never use the StringToPrint variable after you assign it text.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2002
    Posts
    54
    RESOLVED__>>SEE CODE ABOVE
    Last edited by Nintendo_Wizard; Jan 7th, 2003 at 09:41 PM.

  4. #4
    Junior Member
    Join Date
    May 2004
    Location
    England
    Posts
    28

    Red face Noobie

    Sorry for resurrecting this old thread, but it came as part of a search.

    With regards to this comment:

    Also put this code in the PrintDocument _PrintPage Event:
    Code:
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Where do I find that i.e. the PrintDocument_PrintPage Event?

    Also, is anyone aware of a way past the limitations of no multiple pages?

    Thanks.
    On the Level..........

  5. #5
    Lively Member
    Join Date
    Jul 2004
    Location
    Can't say sorry :)
    Posts
    79
    This is also helpfull for me, as it shows me how to print somthing :P.



  6. #6
    Junior Member
    Join Date
    May 2004
    Location
    England
    Posts
    28

    Re: Noobie

    Originally posted by Zoot
    Sorry for resurrecting this old thread, but it came as part of a search.

    With regards to this comment:

    Where do I find that i.e. the PrintDocument_PrintPage Event?
    Ok, I worked that out because I obviously hadn't placed a PrintDocument object on the page

    Originally posted by Zoot

    Also, is anyone aware of a way past the limitations of no multiple pages?

    Thanks.
    Would still like an easy no-frills solution to how to handle more than one page, however if anyone can help.

    Cheers.
    On the Level..........

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