Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi please i need help on this , i am trying to create a button that will print my information from my daatagridview. below is the code i have inputed but when i try it it give a n error and the error is . {"Object reference not set to an instance of an object."} NullReferenceException unhandled
VB
Private Sub PrintToolStripButton_Click(sender As Object, e As EventArgs) Handles PrintToolStripButton.Click
    PrintDialog1.ShowDialog()
    PrintDialog1.Document = PrintDocument1
    PrintDocument1.DocumentName = "Attendance"
    PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim ColumnCount As Integer = AttendanceDataGridView.ColumnCount
    Dim RowCount As Integer = AttendanceDataGridView.RowCount

    Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top

    For Row = 0 To RowCount - 1

        Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left

        For Cell = 0 To ColumnCount - 1

            Dim CellValue As String = AttendanceDataGridView.Rows(Row).Cells(Cell).Value.ToString()
            Dim CellWidth = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Width + 50
            Dim CellHeight = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Height

            Dim Brush As New SolidBrush(Color.Black)
            e.Graphics.DrawString(CellValue, New Font("Century Gothic", 10), Brush, CellLeftPos, CellTopPos)
            e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

            CellLeftPos += CellWidth
        Next

        CellTopPos += AttendanceDataGridView.Rows(Row).Cells(0).Size.Height
    Next
End Sub
Posted
Updated 23-Sep-15 3:35am
v2
Comments
CHill60 23-Sep-15 9:45am    
Which line produces that exception?
Masta Pee 25-Sep-15 4:21am    
@chill60 please the exception is created on
Dim CellValue As String=AttendanceDataGridView.Rows(Row).Cells(Cell).Value.ToString()
on that line
Wombaticus 23-Sep-15 12:13pm    
Most likely you haven't re-populated the grid on postback...
Masta Pee 25-Sep-15 4:35am    
please i dont get you... but please can you help me create a print button on vb that print my datagridview even if its empty or full
Wombaticus 25-Sep-15 5:31am    
My mistake - I was thinking it was an asp.net application. Anyway, the first thing you need to do is figure out exactly where the error is occurring. Add a breakpoint after the showdialog line then hit F5 to run the program. It will stop at the breakpoint and then step through your code one line at a time by hitting F11 til you hit the error.

1 solution

The problem is occurring because one of the cells you are trying to handle does not have a value so the .Value.ToString() call is throwing an exception.

To get around this just check for the cell containing something before attempting to use the Value. For example
VB
For Cell = 0 To ColumnCount - 1

    If Not AttendanceDataGridView.Rows(Row).Cells(Cell).Value Is Nothing Then
        Dim CellValue As String = AttendanceDataGridView.Rows(Row).Cells(Cell).Value.ToString()
        Dim CellWidth = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Width + 50
        Dim CellHeight = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Height

        Dim Brush As New SolidBrush(Color.Black)
        e.Graphics.DrawString(CellValue, New Font("Century Gothic", 10), Brush, CellLeftPos, CellTopPos)
        e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

        CellLeftPos += CellWidth
    End If

Next
 
Share this answer
 
v2
Comments
Masta Pee 25-Sep-15 8:56am    
thank you soo much it worked well.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900