0

I tried convert to string value of cell in a datagridview:

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

MessageBox shows correctly value but application gives exception:

Object reference not set to an instance of an object.
John Saunders
  • 159,224
  • 26
  • 237
  • 393
bred_one
  • 147
  • 3
  • 15

1 Answers1

3

Value property could be null; Try this

string t = row.Cells[0].Value == null ? String.Empty : row.Cells[0].Value.ToString();
Sriram Sakthivel
  • 69,953
  • 7
  • 104
  • 182