Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 6: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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 490
1 Sergey Alexandrovich Kryukov 316
2 Shai Vashdi 264
3 OriginalGriff 250
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid