Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,I am working on my Project in Windows Forms and what I have to do is to get data from Excel File and display this data in datagridview. If in dgv Row is elected, I have to get data from selected Row and send it to Html Document and than I need to Print that Html Document. Also printing and evrything is working but I need now to diplay Printdialog and Prinview window before Print starts and I donn't know how to do that, where have I to insert Printdialog Method?. In my Case Printing start direct when I press a Button, but I want to display Prindialog and Printview window too. Can someone help me with this ? here's my Code:

C#
//Here I am getting Selected Row values
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
    string treqingi, gvari, saxeli, telefoni, tarigi, wona;
    treqingi = row.Cells[0].Value.ToString();
    gvari = row.Cells[1].Value.ToString();
    saxeli = row.Cells[2].Value.ToString();
    telefoni = row.Cells[3].Value.ToString();
    tarigi = row.Cells[4].Value.ToString();
    wona = row.Cells[5].Value.ToString();

    InsertingVariableIntoHtml(treqingi, saxeli, gvari, telefoni, wona);
}

// HTML page Printing
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();

// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
    new WebBrowserDocumentCompletedEventHandler(PrintDocument);

// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(System.IO.Directory.GetCurrentDirectory() + "\\intel\\main.html");

}
C#
private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Print the document now that it is fully loaded.
            ((WebBrowser)sender).Print();

            // Dispose the WebBrowser now that the task is complete. 
            ((WebBrowser)sender).Dispose();
        }
Posted
Comments
Duncan Edwards Jones 3-Jan-16 7:32am    
Why are you using HTML to do the print part for a windows forms application?

There are lots of good examples for printing data grids here that don't use HTML - I think that is a better route to go down?
dave_bulac 3-Jan-16 9:31am    
No, in this case I have allready made my Project and I donn't want to change anything because everything is working good.

1 solution

Probably you mean "print preview". No, you don't want to show print preview "before print starts", it would not make any sense. You should show it when the user wants; and then the user may or may not chose to print. Print preview can be shown using the class PrintPreviewDialog:
PrintPreviewDialog Class (System.Windows.Forms)[^].

If the user wants to print without print preview, this person should be able to do that. But in all cases, if the user chooses to print, the printer and printer settings needs to be chosen through PrintDialog: PrintDialog Class (System.Windows.Controls)[^].

Basically, this MSDN page explains how to do it all: How to: Print in Windows Forms Using Print Preview[^].

—SA
 
Share this answer
 
Comments
dave_bulac 3-Jan-16 17:22pm    
Thanks for advice
Sergey Alexandrovich Kryukov 4-Jan-16 0:40am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900