Solved

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

Posted on 2009-04-01
9
1,041 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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