Solved

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

Posted on 2009-04-01
9
1,037 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 53
Not showing page correctly 3 29
C# Json POSt to Rest API 4 31
WCF Service Application cannot connect from TCP terminal 1 9
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

948 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

21 Experts available now in Live!

Get 1:1 Help Now