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