I tried this code to print from datagridview but it show me error at this line in line += cell.Value.ToString
the eroro
Object reference not set to an instance of an object.
How to fix this problem
vb Code:
  1. Private Sub Button1_Click
  2.  Dim PrintDoc As New PrintDocument
  3.         AddHandler PrintDoc.PrintPage, AddressOf Me.PrintDGV
  4.         PrintDoc.Print()
  5. End Sub
vb Code:
  1. Private Sub PrintDGV(ByVal sender As Object, _
  2.   ByVal ev As PrintPageEventArgs)
  3.         Dim line As String
  4.         For Each row As DataGridViewRow In DataGridView1.Rows
  5.             line = String.Empty
  6.             For Each cell As DataGridViewCell In row.Cells
  7.                 line += cell.Value.ToString
  8.             Next
  9.             ' Render line to printer here
  10.             Dim printFont As New System.Drawing.Font("Arial", 15, System.Drawing.FontStyle.Regular)
  11.             Dim y As Integer
  12.             y += 20
  13.             ev.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black, 10, y)
  14.         Next
  15.     End Sub