Click here to Skip to main content
11,428,497 members (64,429 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
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 Wink | ;)


http://s14.postimage.org/6onchp78x/tests.jpg
Posted 29-Jan-13 7:16am
Comments
Sandeep Mewara at 29-Jan-13 12:26pm
   
What have you tried so far? Where are you stuck?
zakirox123 at 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 at 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
Rate this: bad
good
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:
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
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