[2005] Problem with printing
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 Sub
vb 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
Re: [2005] Problem with printing
How many references are there on that line? There are three, so it really can't be that hard to work out. There's 'line', which we know is not Nothing because you assigned it a value two lines before. There's cell, which we know is not Nothing because you assigned it a value one line before. The only other option is cell.Value. If there's no Value in the cell then you can't call ToString on it. An object that doesn't exist doesn't have a ToString method.
Re: [2005] Problem with printing
I tried this without ToString " line += cell.Value" and it succeed with me.
I want to print with column headeres and with lines of column?
Re: [2005] Problem with printing
Merrion has posted a submission in the VB.NET CodeBank forum that does all the heavy lifting when printing a grid.