Solved

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

Posted on 2009-04-01
9
1,034 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 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now