Is it possible to print a DataGridView without a binding source? If yes, how?

Kennyqui 216 Reputation points
2021-12-18T05:36:59.023+00:00

Sorry about this question I'm beginners.

Developer technologies | Windows Forms
Developer technologies | Visual Basic for Applications
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2021-12-18T17:09:43.27+00:00

    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
    158772-1.png

    Running - Form1
    158683-2.png

    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  
      
    
    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-12-18T23:22:56.013+00:00

    See the following the following code sample, bottom of page has source code in a zip file. Code handles multiple pages, print preview more.

    158775-advmpp5.png

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.