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:
Private Sub Button1_Click Dim PrintDoc As New PrintDocument AddHandler PrintDoc.PrintPage, AddressOf Me.PrintDGV PrintDoc.Print() End Subvb Code:
Private Sub PrintDGV(ByVal sender As Object, _ ByVal ev As PrintPageEventArgs) Dim line As String For Each row As DataGridViewRow In DataGridView1.Rows line = String.Empty For Each cell As DataGridViewCell In row.Cells line += cell.Value.ToString Next ' Render line to printer here Dim printFont As New System.Drawing.Font("Arial", 15, System.Drawing.FontStyle.Regular) Dim y As Integer y += 20 ev.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black, 10, y) Next End Sub


Reply With Quote
