Saturday, January 8, 2011

Printing Datagridview using VB.Net

There will be situations to just print a DataGridView in a application than designing again the columns and rows in a Crystal Report. But in .Net there is no straight forward way to do this or in other words this is not supported natively. To print using an all in one printers, the Datagridview we need to draw all the objects using Graphics Object given in the PrintDocument before calling print method in the respective PrintDocument.

PrintDocument and the Graphics Object

So as I earlier said we need to actually draw all including text using the Graphics object. The print document needs to be prepared before actual printing starts. So we can use the event PrintPage, in this event we can draw all the objects as we need.

Draw the DataGridView like a table using lines

To get a tabular data to represent the DataGridView, we need to do the following

• Draw an outer rectangle as the border to table
• Draw a line for every row
• Draw a line for every column while iterating the cell
• Draw the cell value as text
All the above said items can be drawn with the help of the DataGridView’s native properties, Left, Top, Height, Width, Row.Hieght, and Column. Width

Refer source code VB.Net Print Datagridview in WinForms

0 comments:

Post a Comment