By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

VB.Net 2008 Save Print Preview DataGridView inside txt file or Excel

100+
P: 115
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.

Expand|Select|Wrap|Line Numbers
  1. Dim MyDataGridViewPrinter As DataGridViewPrinter
  2.  
  3.  
  4.     Private Function SetupThePrinting() As Boolean
  5.         Dim MyPrintDialog As PrintDialog = New PrintDialog()
  6.  
  7.         MyPrintDialog.AllowCurrentPage = False
  8.         MyPrintDialog.AllowPrintToFile = False
  9.         MyPrintDialog.AllowSelection = False
  10.         MyPrintDialog.AllowSomePages = True
  11.         MyPrintDialog.PrintToFile = False
  12.         MyPrintDialog.ShowHelp = False
  13.         MyPrintDialog.ShowNetwork = False
  14.  
  15.         PrintDocument1.PrinterSettings = MyPrintDialog.PrinterSettings
  16.         PrintDocument1.DefaultPageSettings.Margins = New Margins(10, 10, 10, 10)
  17.  
  18.         MyDataGridViewPrinter = New DataGridViewPrinter(DataGridView1, PrintDocument1, False, True, "Manager", New Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
  19.  
  20.         Return True
  21.     End Function
  22.  
  23.     Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  24.         Dim more As Boolean
  25.  
  26.         more = MyDataGridViewPrinter.DrawDataGridView(e.Graphics)
  27.         If more Then e.HasMorePages = True
  28.     End Sub
  29.  
  30.  
  31.     ' The Print Preview Button
  32.     Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
  33.         If SetupThePrinting() Then
  34.             Dim MyPrintPreviewDialog As PrintPreviewDialog = New PrintPreviewDialog()
  35.             MyPrintPreviewDialog.Document = PrintDocument1
  36.             MyPrintPreviewDialog.ShowDialog()
  37.         End If
  38.     End Sub
  39.  
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.
May 15 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.