Solved

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

Posted on 2009-04-01
9
1,053 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 100 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 25 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
Technology Partners: 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 100 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 100 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 100 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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