Results 1 to 4 of 4

Thread: [2005] Problem with printing

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    [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:
    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2004
    Location
    syria
    Posts
    854

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width