简体   繁体   English

更改单元格时检查datagridview值是否为空

[英]Check if datagridview value is not null when changing cell

I'm using a CellEndEdit Event for capturing currentCell Value when I'm leaving from it.当我离开它时,我正在使用CellEndEdit事件来捕获 currentCell 值。 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 .问题是,如果用户删除所有单元格并尝试移动到另一个单元格,我会收到错误Exception thrown System.NullReference

Here is my code which I tell that if the cell is null or empty do nothing这是我的代码,我告诉它如果单元格为 null 或为空,则什么都不做

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 .上面的任何内容都不起作用我得到了Null reference Is there any other to check if my current cell doesn't have something inside?还有其他可以检查我当前的单元格内部是否没有东西的吗?

Perhaps Value is null.也许Value为空。 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()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM