views:

5927

answers:

7

I am fairly new to .NET and C#, but I have a DataGridView that I would like to print. What would be the best way to go about doing so?

Thanks in advance!

+1  A: 

There is no built-in print support I'm afraid.

You might resort to using a third party control such as the Infragistics WinGrid which has build-in support for printing.

Gerrie Schenck
@Gerrie Schenck Thanks for the link! I think for now I'll keep pushing to try to use the current control, but I'll keep this in mind incase I need it!
Tina Orooji
+2  A: 

.NET WinForm controls (like the datagridview) do not natively support being printed.

In the case of the datagridview, though, you can call 'DrawToBitmap', and then take that bitmap and pass it on to the printing functions in order to produce it on paper.

GWLlosa
@GWLlosa Thanks! This works and I'm able to print my grid, but it comes out looking like a screenshot. I was hoping to be able to format it in to something a little bit more friendly looking. Thoughts?
Tina Orooji
If you want to reformat the way it looks, you either need to reformat the control itself before the call, or actually 'build your own' datagridview in drawing code, using the data in the rows and cells to populate it. Gets messy quick.
GWLlosa
+4  A: 

There are projects on CodeProject that have done some work printing DataGridViews.

Bob
@Bob Thank you! I'll try implementing this now...
Tina Orooji
@Bob +1 for a great resource.
SoftwareGeek
+2  A: 

You could export the DataGridView to Excel and then print it from Excel.

You could also consider to not show your data in a DataGridView, but show it in a ReportViewer control, which has the ability to export to PDF or Excel. From there it's possible to print your data.

Bliek
@Bliek Thanks for the suggestion! Like I mentioned in another comment, I'd like to try to stick with this control for now... but I'll keep that in mind!
Tina Orooji
I ended up doing this, after all. I still display my data in the DataGridView, but if the user chooses to print it I use a Report Viewer. Thanks again!
Tina Orooji
A: 

If you are going to be printing more than just DataGridViews, then a more generic approach may be worth pursuing. We use MigraDoc and then wrote our own class to read DataGridViews and output MigraDoc classes representing a table.

There are lots of great printing packages available, but I only have experience with MigraDoc.

Edit:

In response to comments, here is a link to my site showing the code I created for generating MigraDoc tables and an example of using it to display a DataGridView (DataGridView to MigraDoc tables).

Robert Gowland
Hi there. Can you share some code samples that use Migradoc with DataGridViews?
edosoft
Updated my answer with a link. Hope this helps!
Robert Gowland
+2  A: 

i can help you tina... open c# project file (new), add a datagridview,one printdocument, one button

button click events
{
printDocument1.Print();
}
printDocument1_PrintPage events
{
Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
            this.dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
            e.Graphics.DrawImage(bm, 0, 0);
}

thats all your data printing:D

i can help all of you if you want everyone;)my msn is vagabondage[at]windowslive.com

+2  A: 

I know you've already accepted an answer, but for the next person to search this question...

I also found this wonderful project on Code Project, and just implemented it. It was EASY and nice. http://www.codeproject.com/KB/grid/GridDrawer.Net.aspx

David Stratton
Thank you for adding on! :o)
Tina Orooji