Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi;

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 ;)


http://s14.postimage.org/6onchp78x/tests.jpg
Posted
Comments
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 :(

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
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);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
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)
{
CaptureScreen();
printDocument1.Print();
}
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.
—SA

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:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx[^].

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,
—SA
 
Share this answer
 
 
Share this answer
 

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