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 the Datagridview we need to draw all the objects using Graphics Object given in the PrintDocument before calling print method in the respective PrintDocument.
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.
Imports System.Data.SqlClient
Imports System.Drawing.Printing
Public Class Form1
Private Const FontAdjustmentFactor = 1.1
Private Const ConnectionString
As String =
"Server=.;" & _
"Database=NorthWind;Trusted_Connection=True" Private ReadOnly Property Connection()
As SqlConnection
Get
Dim ConnectionToFetch
As New SqlConnection(ConnectionString)
ConnectionToFetch.Open()
Return ConnectionToFetch
End Get
End Property Public Function GetData()
As DataView
Dim SelectQry =
"SELECT CategoryName,Description FROM Categories " 'Dim SelectQry = "SELECT * FROM products " Dim SampleSource
As New DataSet
Dim TableView
As DataView
Try Dim SampleCommand
As New SqlCommand()
Dim SampleDataAdapter =
New SqlDataAdapter()
SampleCommand.CommandText = SelectQry
SampleCommand.Connection = Connection
SampleDataAdapter.SelectCommand = SampleCommand
SampleDataAdapter.Fill(SampleSource)
TableView = SampleSource.Tables(0).DefaultView
Catch ex
As Exception
Throw ex
End Try Return TableView
End Function Private Sub btnLoad_Click(
ByVal sender
As System.
Object, _
ByVal e
As System.EventArgs)
Handles btnLoad.Click
DataGridView1.DataSource = GetData()
End Sub Private Sub GridViewPrintDocument_PrintPage(
ByVal sender
As Object, _
ByVal e
As PrintPageEventArgs)
PrintGridView(e.Graphics)
End Sub Private Sub btnPrint_Click(
ByVal sender
As Object, _
ByVal e
As System.EventArgs)
Handles btnPrint.Click
Dim GridViewPrintDocument
As New Printing.PrintDocument
AddHandler GridViewPrintDocument.PrintPage, _
AddressOf GridViewPrintDocument_PrintPage
GridViewPrintDocument.Print()
End Sub Private Sub PrintGridView(
ByVal GxPrint
As Graphics)
DrawGridViewBox(GxPrint)
DrawGridViewHeader(GxPrint)
DrawGridViewRows(GxPrint)
End Sub Private Sub DrawGridViewHeader(
ByVal GxPrint
As Graphics)
Dim CellText =
String.Empty
Dim StartTop = DataGridView1.Top
Dim PrintFont
As New Font(
New FontFamily(
"Microsoft Sans Serif"), _
10, FontStyle.Bold)
Dim StartLeft = DataGridView1.Left
For Each PrintCol
As DataGridViewColumn In DataGridView1.Columns
CellText = PrintCol.HeaderText
GxPrint.DrawString(CellText, PrintFont, Brushes.Gray, _
StartLeft, StartTop)
GxPrint.DrawLine(Pens.Black, StartLeft, StartTop, _
StartLeft, StartTop + DataGridView1.Rows(0).Height)
StartLeft += PrintCol.Width * FontAdjustmentFactor
Next GxPrint.DrawLine(Pens.Black, DataGridView1.Left, _
StartTop + DataGridView1.Rows(0).Height, _
CInt(DataGridView1.Width * FontAdjustmentFactor), _
StartTop + DataGridView1.Rows(0).Height)
End Sub Private Sub DrawGridViewRows(ByVal GxPrint As Graphics)
Dim RowIndex = 1 'since header is used so we start with 1
Dim PrintFont As New Font(New FontFamily("Microsoft Sans Serif"), _
10, FontStyle.Regular)
For Each PrintRow As DataGridViewRow In DataGridView1.Rows
Dim StartTop = DataGridView1.Top + (RowIndex * PrintRow.Height)
Dim ColIndex = 0
Dim StartLeft = DataGridView1.Left
For Each PrintCell As DataGridViewCell In PrintRow.Cells
StartLeft *= FontAdjustmentFactor
Dim CellText = String.Empty
If (Not IsDBNull(PrintCell.Value)) Then CellText = PrintCell.Value
GxPrint.DrawString(CellText, PrintFont, Brushes.Gray, _
StartLeft, StartTop)
GxPrint.DrawLine(Pens.Black, StartLeft, StartTop, _
StartLeft, StartTop + PrintRow.Height)
StartLeft += DataGridView1.Columns(ColIndex).Width
ColIndex += 1
Next
GxPrint.DrawLine(Pens.Black, DataGridView1.Left, _
StartTop + PrintRow.Height, _
CInt(DataGridView1.Width * FontAdjustmentFactor), _
StartTop + PrintRow.Height)
RowIndex += 1
Next
End Sub
Private Sub DrawGridViewBox(ByVal GxPrint As Graphics)
Dim GridViewRect As New Rectangle( _
DataGridView1.Left, DataGridView1.Top, _
DataGridView1.Width * FontAdjustmentFactor, _
DataGridView1.Height * FontAdjustmentFactor)
GxPrint.DrawRectangle(Pens.Black, GridViewRect)
End Sub
End Class