I’m using VB.Net 2008 application program.

I’m using DataGridView. I have a Print Preview option where i need to print the DataGridView.

Using "DataGridView1.Rows.Add"  i’m adding rows to datagridview.

I’m using this code for Print Preview.

Dim MyDataGridViewPrinter As DataGridViewPrinter

Private Function SetupThePrinting() As Boolean
    Dim MyPrintDialog As PrintDialog = New PrintDialog()

    MyPrintDialog.AllowCurrentPage = False
    MyPrintDialog.AllowPrintToFile = False
    MyPrintDialog.AllowSelection = False
    MyPrintDialog.AllowSomePages = True
    MyPrintDialog.PrintToFile = False
    MyPrintDialog.ShowHelp = False
    MyPrintDialog.ShowNetwork = False

    PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
    PrintDocument1.DefaultPageSettings.Margins = New Margins(10, 10, 10, 10)

    MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, False, True, "Manager", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)

    Return True
End Function

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim more As Boolean

    more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
    If more Then e.HasMorePages = True
End Sub

' The Print Preview Button
Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
    If SetupThePrinting() Then
        Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
        MyPrintPreviewDialog.Document = PrintDocument1
    End If
End Sub

I searched a lot, but I couldn’t find it.

If you know how i can Save Print Preview inside text file or Excel, please help me. If you can provide an example, that will be so helpful.

Thanks in advance.

Edited by ~s.o.s~: Code tags

4 Years
Discussion Span
Last Post by Begginnerdev

You will have to create a custom print method.

In this method you will have to loop through the datagridview and parse everything line by line.
You will have to pass each line as a String variable.

You can use this as a reference.

It is a tedious process. One you master it, you will be able to use it on any project with a gridview/listview.

Good luck, friend.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.