tags:

views:

57

answers:

2

I need to print the contents of a datagridview, simple text in rows and columns, with GDI+ in .NET to paper.

To do this, I need to iterate over the rows, draw lines, iterate over the columns, calculate width, height, wrap the contents if contents do not fit on one line, etc. This is all possible, but is there a library that simplifies the drawing of tables in GDI+ and abstracts some of the difficulties away?

A: 

I've implemented this using XML and XslCompiledTransform.

You need to build your XML (easiest way is using LINQ2XML) and then have your XSLT build the HTML table by enumerating the values your XML produced. You can then represent this in a WebBrowser control, and tell the page to print with WebBrowser.ShowPrintPreviewDialog.

The advantage of using the WebBrowser is having all the page layout tools provided to you so you don't need to worry about it.

Pasting my own implementation would be huge, but please let me know if you need anything clarifying.

Of course, if you have a fixed number of columns, why not try represent it on a report?

Codesleuth
Nice solution. I need to do this in GDI+ though, because it is part of an existing printout.
Jan Willem B
+2  A: 

This is a very common request. Easy keywords, google "print datagridview". Here's a decent looking one.

Hans Passant
Thanks. I will look into it.
Jan Willem B
not exactly an answer to the question (looking for a library to simplify things), but the link provides a useful example.
Jan Willem B