建立了一个数据连接,Data GridView里面也有数据显示,就是我用下面的方法取值时提示错误
未将对象应用设置到对象实例化,
MessageBox.Show(this.dataGridView1.Rows[2].Cells[1].Value.ToString())
是什么原因?

解决方案 »

  1.   

    这个元素可能是DBNull,把你显示的DataGridView贴出来
      

  2.   

    先调试一下
    this.dataGridView1.Rows[2].Cells[1].Value 是多少
      

  3.   

    未将对象应用设置到对象实例化
    ------------------------------
    语句没写错。
    很明显语句执行时某个变量引用的对象为空(null)才报这样的异常。
    你设断点看看是哪个值为null。
      

  4.   

    this.dataGridView1.Rows[2].Cells[1]中没有值
      

  5.   

    MessageBox.Show(Convert.ToString(this.dataGridView1.Rows[2].Cells[1].Value))
    这样不会报错了