Hi
Yes, it is possible.
Below, some over simplified code to illustrate a simple example.
NOTE: this example doesn't take care of many aspects of a print job that an actual application may need to see to (such as: this example doesn't cater for multi page data size)
If you want to try this example, you will need a DataGridView1 with columns Column1, Column2, Column3 and Column4, and add components PrintDocument1, PageSetUpDialog1, PrintDialog1 and PrintPreviewDialog1 in the Designer.
Designer - Form1
Running - Form1
Code
' GataGridView1 with columns
' One, Two, Three and Four
' this example has not catered for
' multi page data size
Option Strict On
Option Explicit On
Public Class Form1
' set font for printing
Dim txtFont As New Font("Arial", 8)
' just used to create dummy data
Dim rand As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' add some dummy data to DGV
Dim fruit As New List(Of String)({"Kiwi", "Orange", "BlueBerry", "Banana", "Papaya", "Durian", "Grape"})
For i As Integer = 0 To 19
With DataGridView1
.Rows.Add(fruit(rand.Next(0, fruit.Count)), rand.NextDouble() * rand.Next(19, 99), rand.Next(99, 999), rand.Next(999, 9999))
End With
Next
With DataGridView1
' format a large width double number
' to a manageable 2 decimals
.Columns("Column2").DefaultCellStyle.Format = "0.00"
' set column to right alignment
.Columns("Column2").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
End With
setupPrintDoc()
End Sub
Sub setupPrintDoc()
' just to add document to controls
PageSetupDialog1.Document = PrintDocument1
PrintDialog1.Document = PrintDocument1
PrintPreviewDialog1.Document = PrintDocument1
End Sub
Function Widest(col As Integer, g As Graphics) As Integer
' beware of true value versus
' display value
Dim w As Single = 10
For Each r As DataGridViewRow In DataGridView1.Rows
If Not r.Index = DataGridView1.NewRowIndex Then
Dim ww As Single = g.MeasureString(r.Cells(col).Value.ToString, txtFont).Width
If ww > w Then w = ww
End If
Next
Return CInt(w) + 10
End Function
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
' DO PRINT
Dim x As Integer = e.PageSettings.Margins.Left
Dim y As Integer = e.PageSettings.Margins.Top
Dim ry As Integer = CInt(e.Graphics.MeasureString(DataGridView1(0, 0).Value.ToString, txtFont).Height)
Dim rx As Integer = Widest(0, e.Graphics)
Dim fmt As New StringFormat
fmt.LineAlignment = StringAlignment.Far
For Each r As DataGridViewRow In DataGridView1.Rows
If Not r.Index = DataGridView1.NewRowIndex Then
With e.Graphics
fmt.Alignment = StringAlignment.Near
.DrawString(r.Cells("Column1").Value.ToString, txtFont, New SolidBrush(Color.Black), New Rectangle(x, y, rx, 30), fmt)
x += rx
fmt.Alignment = StringAlignment.Far
.DrawString(CDbl(r.Cells("Column2").Value).ToString("0.00"), txtFont, New SolidBrush(Color.Black), New Rectangle(x, y, rx, 30), fmt)
x += rx
fmt.Alignment = StringAlignment.Far
.DrawString(CDbl(r.Cells("Column3").Value).ToString, txtFont, New SolidBrush(Color.Black), New Rectangle(x, y, rx, 30), fmt)
x += rx
fmt.Alignment = StringAlignment.Far
.DrawString(CDbl(r.Cells("Column4").Value).ToString, txtFont, New SolidBrush(Color.Black), New Rectangle(x, y, rx, 30), fmt)
End With
End If
x = e.PageSettings.Margins.Left
y += ry
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' print dialog
With PrintDialog1
.AllowSomePages = True
.AllowCurrentPage = True
.AllowSelection = True
.AllowPrintToFile = True
.ShowNetwork = True
End With
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Preview
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' page setup
PageSetupDialog1.ShowDialog()
End Sub
End Class