?
Solved

How to print a values of datagrid using c# in windows application

Posted on 2009-04-01
9
Medium Priority
?
1,073 Views
Last Modified: 2012-05-06
I am a newbie to c#.

I dont know how to print the datagrid values?

I have bind the values in datagrid. By clicking a "print" button, I have to print all the records.

Can anyone help me?

 
0
Comment
Question by:herosaravanan
  • 4
  • 4
9 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 400 total points
ID: 24038213
There's a pretty good article on CodeProject:
http://www.codeproject.com/KB/printing/DataGrid_Printing_Class.aspx

To be quite honest, I've never implemented it as I've written my own code to do the same job (In fact my first attempt was incredibly lazy: I wrote code to export to html, and then used the browser to print), but from skimming through the code, it looks like it should to the job.
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 100 total points
ID: 24038641
this article from code project shows how to print a datagridview
http://www.codeproject.com/KB/printing/datagridviewprinter.aspx
0
 

Author Comment

by:herosaravanan
ID: 24039342
I am using a pdf file to print datagrid values.
All the records will write in a pdf file and then saved in  a location then it will be printed.
But all the records were overwrite in the first page.
So after executing the script, only the last record is there.
 Please tell me how to print each record in a page.
like 1st record in 1st page and 2nd in the 2nd page ans  so on.
123.pdf
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Assisted Solution

by:oobayly
oobayly earned 400 total points
ID: 24039523
Funny, I've been doing the exact same today, except using Siberix's PDF writer (our project can't use GPL code).
I've never used iTextSharp, but surely as you're looping through each record to write to the document you can just add a new page to the document, and write the record to the most recent page.
0
 

Author Comment

by:herosaravanan
ID: 24039947
Yes. I know that.But i dont know the exact syntax.

Here is my code

           foreach (DataGridViewRow ds in dataGridView1.Rows)
            {
                Document doc  = new Document();
                PdfWriter.GetInstance(doc, new FileStream( "123.pdf", FileMode.Create));
                doc.Open();
               
              //Some file formating codes here

                doc.NewPage();
                doc.Close();
              }

             Please tell me what is wrong i my code.Why icant able to wrie records in next page.

            Help please....
0
 
LVL 15

Assisted Solution

by:oobayly
oobayly earned 400 total points
ID: 24040183
Aha, what you're doing opening the document, create the page with the record, writing & saving. Then the next record you overwrite the existing pdf file.

Try this:
            Document doc  = new Document();
            PdfWriter.GetInstance(doc, new FileStream( "123.pdf", FileMode.Create));
            doc.Open();
            foreach (DataGridViewRow ds in dataGridView1.Rows)
            {
 
               
              //Some file formating codes here
 
                doc.NewPage();
                
             }
             doc.Close(); 

Open in new window

0
 

Author Comment

by:herosaravanan
ID: 24046790
Thank you for your help.

Now i am able to write all the recods in pdf.

I need another help.

 Is there anyway to print the datagrid values without writing in any file like pdf or word...

Why i am asking this because,  i am creating windows application.

I dont know all the users of my application have acrobat installed in their system..

How can i print the datagrid values into the printer buffer instaed of wrting it in a file and then printing it.

Any help is appreciated.


0
 
LVL 15

Assisted Solution

by:oobayly
oobayly earned 400 total points
ID: 24047703
You'd have to add a PrintDocument component to the form and handle it's PrintPage event. The PrintPageEventArgs contains a Graphics object onto which you can draw text, lines, images etc.
It'll be a little more complicated than generating the PDF as you have to position everything manually, unlike iTextSharp where you can create a table object and write it to a PDF.

This is MSDN's documentation:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

You could then use the Code from the link I suggested in my first reply to paint the grid on the printer's graphics object.
0
 

Author Closing Comment

by:herosaravanan
ID: 31565271
Thanks for your help.Nice service!!! D\Go ahead
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question