我尝试将单元格值分配给字符串时的对象引用异常

时间:2014-01-18 17:49:33

标签: c# datagridview nullreferenceexception

我尝试在datagridview中转换为cell的字符串值:

string t = row.Cells[0].Value.ToString()== null ? String.Empty : row.Cells[0].Value.ToString();
MessageBox.Show(t);

MessageBox正确显示值,但应用程序提供异常:

Object reference not set to an instance of an object.

1 个答案:

答案 0 :(得分:3)

Value属性可以为null;试试这个

string t = row.Cells[0].Value == null ? String.Empty : row.Cells[0].Value.ToString();
相关问题