Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



Reply
VB.Net 2008 Save Print Preview DataGridView inside txt file or Excel
Old 05-14-2012, 11:46 PM VB.Net 2008 Save Print Preview DataGridView inside txt file or Excel
Experienced Talker

Posts: 39
Name: remya
Trades: 0
I知 using VB.Net 2008 application program.

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

Quote:
Using "DataGridView1.Rows.Add" i知 adding rows to datagridview.


I知 using this code for Print Preview.

Code:
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
            MyPrintPreviewDialog.ShowDialog()
        End If
    End Sub


I searched a lot, but I couldn稚 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.
remya1000 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-20-2012, 10:30 AM Re: VB.Net 2008 Save Print Preview DataGridView inside txt file or Excel
Average Talker

Posts: 19
Location: Mumbai , India
Trades: 0
Instead of using a PrintPreview you can

A) Take a Direct Print Screen of the DataGrid and Save it as JPG
Quote:
Dim bt As New Bitmap(DataGridView1.Width, DataGridView1.Height)
Dim gr As Graphics = Graphics.FromImage(bt)

bt.Save("D:\Demot.jpg")
Or

B) Export the DataGridView Directly to a CSV Format or Text Format
Quote:
Dim strW As New StreamWriter(SaveFileDialog1.FileName)
strW.WriteLine("Sr,Name,Phone Number,Email ID,Date of Birth")
For i As Integer = 0 To DataGridView1.Rows.Count - 1
strW.WriteLine(String.Format("{0},{1},{2},{3},{4}" , i + 1, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value, DataGridView1.Rows(i).Cells(3).Value, DateTime.Parse(DataGridView1.Rows(i).Cells(4).Valu e).ToString("dd-MMM-yyyy")))
Next
strW.Close()
Please note
The 2nd Line defines the Columns you want..
Quote:
strW.WriteLine("Sr,Name,Phone Number,Email ID,Date of Birth")
bzwiz is offline
Reply With Quote
View Public Profile Visit bzwiz's homepage!
 
Reply     « Reply to VB.Net 2008 Save Print Preview DataGridView inside txt file or Excel
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.50729 seconds with 11 queries