Click here to Skip to main content
13,355,538 members (74,170 online)
Rate this:
Please Sign up or sign in to vote.
See more:

i am using c# in visual studio 2008. i want to print my datagridview via printer, here i have attached my datagridview. please help me advanced thanks for you all ;)
Posted 29-Jan-13 7:16am
Sandeep Mewara 29-Jan-13 12:26pm
What have you tried so far? Where are you stuck?
zakirox123 29-Jan-13 12:28pm
i used this code but it just print my whole form :(

public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
e.Graphics.DrawImage(memoryImage, 0, 0);
private void printButton_Click(System.Object sender, System.EventArgs e)
Sergey Alexandrovich Kryukov 29-Jan-13 12:50pm
What's the problem then? It looks like you just copied the code. If you do it by yourself, you well see that you can print whatever you need.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First of all, please see my comment to the question.

As you should already know, to print anything in your System.Windows.Forms application, you need to use the class System.Drawing.Printing.PrintDocument:[^].

A simple code sample at the end of this MSDN help page clearly explains how to do it.

But the idea of "printing of control", I think, is related to some usability misconception. In most cases, you need to print data, not the panel, form or something directly reproducing the look of your UI. Paper document is paper document, screen is screen. I'll explain. Consider you have a button and a check box on your container control like a panel. On screen, it can click on check box and change is status, as well as the state of some underlying object, for example, some instance of some data model. If you click on a button, you can get trigger some processing, which can do something unrelated to a current look of your screen, for example, process existing data and generate completely new view out of it. On paper, what can your user to click on? Showing those boxes and buttons can only be confusing. That's why PrintDocument is abstracted from the controls.

At the same time, the look of the DataGridView make look very close to what you want in print. But there are other problems. First, layout should be different. In print, you have to format data to fit the page, which is very different for screen, especially if you need the table to span more than one page.

More importantly, this is a wrong in terms of architecture and isolation of UI layer. You need to have a separate data layer. When you show your grid view and populated it, you do it from the data layer. Likewise, you should create a print from the data layer, not from UI.

Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100