Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
I have a problem with printing windows form! However i am successful in printing the Data-grid of the form but don't have idea to print form I have researched in printing win-forms in forums but could not find the relevant . Almost all project are done in VB.Net but m using Dot net 4 framework! I have a form with 2 date picker 1) From Date 2) to date and a search button . Whenever i click search button the result according to date are shown in grid view and if i click on Print button on my form I can print the grid and i have made a string to print the current date which is also printed but what i want now is to print the content of date picker! For example if i fire print command it must print grid as well as the value of both date picker! How can we achieve printing of forms? If somebody needs codes of printing grid I can provide it unless it is necessary! I am seeking help in printing contents(Customized Contents) of form .

Don't print the form, don't print the data grid. Print data using some data layer behind your view. If you use some data layer bound with your data grid, print using this data. Even if you don't have data binding, it means you should have some code used to populate the data grid and other controls. Use it for printing. Use the class System.Drawing.Printing.PrintDocument, see[^]; this page contains some useful code sample.

Share this answer
Espen Harlinn 18-Jul-11 10:50am    
Good advice, my 5
Sergey Alexandrovich Kryukov 18-Jul-11 11:28am    
Thank you, Espen.
shoebass 22-Jul-11 8:17am    
Dear SAKryukov:

Well I went through the link u gave but still cannot accomplish my work. Could you please provide me code samples ?

Well I also have implemented another method of printing that is by making image of the form but this seems so ugly . though form and grid both are printed its seems like we are printing by taking Print-screen .

And yeas I have used databindings to populate data in datagrid. I am confused about using the class you mentioned to print my desired field . Could u please elaborate more on this?
Sergey Alexandrovich Kryukov 14-Jul-13 23:06pm    
There is a code sample in the referenced MSDN article. Isn't that enough? The problem is not related to data binding. You need just some printed presentation generated from data. How you do it depends on how you want it and is totally your decision.
Member 10918392 3-Jul-14 1:59am    
I want to print the form directly to the printers without previewing the form in c#.
If u have code please give it to me
As you have specified that you want to print customized contents of the form beside DataGridView and also mentioned that you are able to print the datagridview. Some lines of code are here below for print other contents of the form like image, text, date etc.:
// 1)	For printing an image above each page 
// In PrintPage event of PrintDocument1, after setting margins use following code 
string app_path =  System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); // you can change path as required 
Point p2 = new Point(900, 5);	// Display a picture on top of each page 
e.Graphics.DrawImage(Image.FromFile(app_path + "\\Books.ico"), p2); 

// 2)	For Writing any message on top of the pages 
e.Graphics.DrawString("Books List - Subjectwise",
           new Font(dataGridView3.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left - 15,
e.MarginBounds.Top - e.Graphics.MeasureString("Books List - Subjectwise",
new Font(dataGridView3.Font, FontStyle.Bold),
e.MarginBounds.Width).Height - 10);  // 10 is the gap between top margin and Header

// 3)	For header date 
      String strDate = DateTime.Now.ToLongDateString();
      new Font(dataGridView3.Font, FontStyle.Bold), Brushes.Black,
      e.MarginBounds.Left - 15 +
      (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
      new Font(dataGridView3.Font, FontStyle.Bold),
      e.MarginBounds.Width).Width) - 100,
      e.MarginBounds.Top - e.Graphics.MeasureString("Report Date",
      new Font(new Font(dataGridView3.Font, FontStyle.Bold),
FontStyle.Bold), e.MarginBounds.Width).Height - 13);

// 4)	Here you can print your DataGridView contents 

Above Code is written in C#2.0
Hope this will be useful for you.
Share this answer
shoebass 24-Jul-11 1:58am    
Appreciated your method but I have already Printed Header Date and Message on top of the page . What I want to be printed is the value of date-picker that is in my form I am not being able to print the specific date value of date-picker which is actually on the form. Is there any other possible way to print the value of date-picker too? Please help me out! If there was not date-picker the work would have already completed .
You can use 'Text' property of dateTimePicker to store it in DateTime variable after converting it to DateTime type as below:

DateTime Issuedt = Convert.ToDateTime(dateTimePicker1.Text);

Now Issuedt can be used in printPage event.
Share this answer
Its very easy to print any win form in C# .Net.
Goto ToolBox
>Visual Basic PowerPacks
>drag PrintForm Component to the form
use the code to print form on Button Click
private void button1_Click(object sender, EventArgs e)
Share this answer
Jørgen Hede 7-Sep-15 6:55am    
This will only make the printer print a blank paper
!!! THIS IS A C# CODE !!!

you can also add this method to your project and call it like

add "using System.Drawing.Printing;" to any form from where you will call the print function
private void printPreview_Click(object sender, EventArgs e)
    if (ActiveMdiChild == null) return;
    Helper.SendFormToPrint(ActiveMdiChild, PrintAction.PrintToPreview);

private void print_Click(object sender, EventArgs e)
    if (ActiveMdiChild == null) return;
    Helper.SendFormToPrint(ActiveMdiChild, PrintAction.PrintToPrinter);

public static void SendFormToPrint(Form formToPrint, PrintAction target)
           if (formToPrint == null) return;



           var printFormobject = new PrintForm
                                               Form = formToPrint,
                                               PrintAction = target

           var pr = new PrintDialog();
           if (!pr.ShowDialog(formToPrint).Equals(DialogResult.OK)) return;

           printFormobject.PrinterSettings = pr.PrinterSettings;

the print function was put together from various help found on the web.
methods are called form an MDIparent menu(s)
and you get a print dialog to select printer and layout.
Share this answer
Jibesh 16-Jan-13 20:38pm    
why you replied for a question which was answered and resolved almost 2yers ago?
R. Erasmus 14-Jul-13 5:14am    
Lol, I think the question got re-posted somehow.
For printing the form elements in C# you can use the following code..
private void btnPrint_Click(object sender, EventArgs e)
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(PrintImage);
            printPreviewDialog1.Document = pd;

        void PrintImage(object o, PrintPageEventArgs e)
            int x = SystemInformation.WorkingArea.X;
            int y = SystemInformation.WorkingArea.Y;
            int width = this.Width;
            int height = this.Height;

            Rectangle bounds = new Rectangle(x, y, width, height);

            Bitmap img = new Bitmap(width, height);

            this.DrawToBitmap(img, bounds);
            Point p = new Point(100, 100);
            e.Graphics.DrawImage(img, p);

Dont forgat to include(reference) of using "System.Drawing.Printing;"

Controls that have to added are: button,printpreviewdialog,
Share this answer
[no name] 13-Jul-13 17:01pm    
Why you replying to a question which was answered and resolved almost 2 years ago?

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