Continue the post How To Load Data From Database Into DataGridView In VB.NET, I'll show you how to print a DataGridView in Windows Forms Application using VB.NET.
Print a DataGridView sometimes needed in your application, instead you have to design a report with the corresponding column in the DataGridView, then fill your data into the report.
You can print the DataGridView easily by using the PrintDocument component.
From your toolbox, drag a button to your windows form application, then layout as shown below
Add a PrintDocument control from toolbox under printing tab to your windows forms, then double click on it to generate the PrintPage event.
The PrintDocument control is used to set the properties that describe what to print and then to print the document within windows-based applications. It can be used in conjunction with the PrintDialog component to be in command of all aspects of document printing.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim db As New NorthwindEntities EmployeeBindingSource.DataSource = db.Employees.ToList() End Sub Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim dataGridViewImage As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height) DataGridView1.DrawToBitmap(dataGridViewImage, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height)) e.Graphics.DrawImage(dataGridViewImage, 0, 0) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click PrintDocument1.Print() End Sub End Class
Run your project, then click Print button to print your DataGridView