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
  • 160,644
  • 26
  • 247
  • 397
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
  • 72,067
  • 7
  • 111
  • 189