VB.Net - Print Preview A DataGridView Control

The following code snippet demonstrates how to print preview a DataGridView control, using the PrintDocument and PrintPreviewDialog classes.
  1. Imports System.Drawing
  2. Imports System.Drawing.Printing
  3. Imports System.Data
  4. Imports System.Data.SqlClient
  5.  
  6. Public Class Form1
  7.  
  8.     Dim EmpGrid As New DataGridView()
  9.     Dim PrintDocument1 As New PrintDocument()
  10.     Dim PrintPreviewDialog1 As New PrintPreviewDialog()
  11.  
  12.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  13.  
  14.         Dim Provider As String = "Data Source=_DATA_SOURCE_HERE_;Initial Catalog=AdventureWorks;Integrated Security=True"
  15.         Dim Connection As New SqlConnection(Provider)
  16.  
  17.         Try
  18.             Connection.Open()
  19.  
  20.             Dim Command As New SqlCommand("SELECT TOP 20 FirstName, MiddleName, LastName, EmailAddress FROM Person.Contact", Connection)
  21.             Dim Adapter As New SqlDataAdapter(Command)
  22.             Dim EmpTable As New DataTable()
  23.             Adapter.Fill(EmpTable)
  24.  
  25.             EmpGrid.DataSource = EmpTable
  26.             PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
  27.             PrintPreviewDialog1.Document = PrintDocument1
  28.             PrintPreviewDialog1.Show()
  29.  
  30.             AddHandler PrintDocument1.PrintPage, AddressOf PrintDocument1_PrintPage
  31.  
  32.         Catch Ex As Exception
  33.             MessageBox.Show(Ex.Message)
  34.         End Try
  35.  
  36.         EmpGrid.Dock = DockStyle.Fill
  37.         Me.Controls.Add(EmpGrid)
  38.         EmpGrid.AutoResizeColumns()
  39.  
  40.     End Sub
  41.  
  42.     Protected Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
  43.         Dim ColumnCount As Integer = EmpGrid.ColumnCount
  44.         Dim RowCount As Integer = EmpGrid.RowCount
  45.  
  46.         Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top
  47.  
  48.         For Row = 0 To RowCount - 2
  49.  
  50.             Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left
  51.  
  52.             For Cell = 0 To ColumnCount - 1
  53.  
  54.                 Dim CellValue As String = EmpGrid.Rows(Row).Cells(Cell).Value.ToString()
  55.                 Dim CellWidth = EmpGrid.Rows(Row).Cells(Cell).Size.Width + 10
  56.                 Dim CellHeight = EmpGrid.Rows(Row).Cells(Cell).Size.Height
  57.  
  58.                 Dim Brush As New SolidBrush(Color.Black)
  59.                 e.Graphics.DrawString(CellValue, New Font("arial", 9), Brush, CellLeftPos, CellTopPos)
  60.                 e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)
  61.  
  62.                 CellLeftPos += CellWidth
  63.             Next
  64.  
  65.             CellTopPos += EmpGrid.Rows(Row).Cells(0).Size.Height
  66.         Next
  67.  
  68.     End Sub
  69.  
  70. End Class

1 comment:

Jithu Wilson C said...

This is the only one post that worked in printing Datagridview directly without Report.

Post a Comment