Print a DataGridView in VB.Net
VB.Net Tutorial:Datagridview Printing in 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. This article is based on article from VB.Net Tutorial. To print 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.
This is a versatile piece of the functionality of Datagridview – if you want to print out your data anyway. This tutorial should help you with the DataGridView – now you know what your broadband (http://www.o2.co.uk/broadband/) is for – great VB.net tips. You will be able to see your data looking organised, which will enable you to extract exactly what you want. Good luck with this.
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
You can use graphics object GxPrint to draw text lines and strings as follows
GxPrint.DrawString(CellText, PrintFont, Brushes.Gray, StartLeft, StartTop)
GxPrint.DrawLine(Pens.Black, StartLeft, StartTop, StartLeft, StartTop + PrintRow.Height)