[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Print DataGridView

Posted on 2006-05-18
5
Medium Priority
?
1,449 Views
Last Modified: 2008-01-09
hi
I have a c# windows program which will query data from database, and show it on the dataGridView
I want to create a button so that I can click on the button to print the dataGridView or dataTable
how can I do that?

thanks
0
Comment
Question by:Vik Shah
5 Comments
 
LVL 11

Expert Comment

by:vo1d
ID: 16710957
you will have to do all your drawing by yourself using the graphics object if a printdocument.
to get more information, which objects you need to get to the point, where you have to to your drawing, take a look at this link:
http://www.c-sharpcorner.com/winforms/texteditor.asp
0
 

Author Comment

by:Vik Shah
ID: 16711433
I check the link, I have a similar c# code sample from same editor (Michael Gold)
http://www.c-sharpcorner.com/Graphics/DataGridPrinterMG.asp
 I  look at the code, it uses DataGrid instead of DataGridView
and DataSet instead of DataTable. I modify the code to use DataGridView and DataTable.

The structure of my program is :
Several DataGridViews in a  windows form ,
Each of them has a DataTable as DataSource
Next to each view, there is a print button
the print button will call
            dataGridPrinter1 = new DataGridPrinter(DataGridView1, printDocument1, DataTable1);
            dataGridPrinter1.PageNumber = 1;
            dataGridPrinter1.RowCount = 0;
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument1.Print();
            }

but when I try to preview or print, it only shows a blank page.

What could possibly go wrong?

Thanks a lot

By the way,  are you Michael Gold ?
0
 
LVL 11

Assisted Solution

by:vo1d
vo1d earned 300 total points
ID: 16711656
no, im not michael gold :)
you only got a blanc page because you did not draw your grid. you must
use the printpage event of the printdocument like this and make your drawing of all the grid data
in his example, it looks like that:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
  Graphics g = e.Graphics;
// Draw  a label title for the grid
  DrawTopLabel(g);

// draw the datagrid using the DrawDataGrid method passing the Graphics surface
  bool more = dataGridPrinter1.DrawDataGrid(g);

// if there are more pages, set the flag to cause the form to trigger another print page event
  if (more == true)
   {
     e.HasMorePages = true;
     dataGridPrinter1.PageNumber++;
   }
}

-------
DrawTopLabel and dataGridPrinter1.DrawDataGrid are some methods, which draws all the data into the printdocument graphics obect, in hsi example called 'g'.
so you will have to implement that methods too.
0
 
LVL 8

Expert Comment

by:gena17
ID: 16711915
Try out to use Microsoft Reporting Services.
It gives you a lot of flexibility and you don't need to paint it yourself.
0
 
LVL 2

Accepted Solution

by:
salan_alani earned 450 total points
ID: 16724184
I posted a C# class to print a DataGridView, have a look to the article, class (.cs) file, and a demo project in the following url:

http://www.codeproject.com/csharp/datagridviewprinter.asp


Hope it will help you..


Salan
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

873 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