-1

I'm using a CellEndEdit Event for capturing currentCell Value when I'm leaving from it. The problem is that, if the user deletes all the cell and trying to move to another cell I'm getting error Exception thrown System.NullReference.

Here is my code which I tell that if the cell is null or empty do nothing

I tried:

 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex].Value != "")
 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex].Value != null)
 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex].Value.ToString() != "")
 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex].Value.ToString() != "")
 if ( !String.IsNullOrWhiteSpace(AppointmentGrid[e.ColumnIndex, e.RowIndex].Value.ToString()))

Nothing from above doesn't work I get the Null reference. Is there any other to check if my current cell doesn't have something inside?

FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46
Dim
  • 41
  • 8

1 Answers1

0

Perhaps Value is null. You can use the null propagation operator ? to avoid the exception.

 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex]?.Value != "")
 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex]?.Value != null)
 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex]?.Value.ToString() != "")
 if ( AppointmentGrid[e.ColumnIndex, e.RowIndex]?.Value.ToString() != "")
 if ( !String.IsNullOrWhiteSpace(AppointmentGrid[e.ColumnIndex, e.RowIndex]?.Value.ToString()))
pmcilreavy
  • 3,076
  • 2
  • 28
  • 37