private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
                 rowIndex = e.RowIndex;
                 int ROWC = dataGridView1.Rows.Count;
                 if (rowIndex != -1 && rowIndex < ROWC - 1)
                 {
                     try
                     {
                         XMforR = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString();                         if (dataGridView1.Rows[rowIndex].Cells[0].Value.ToString() != "")
                         {
                             MessageBox.Show(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());
              XMforT = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
                         }
                     }
                     catch (Exception ex)
                     {
                         MessageBox.Show(ex.Message.ToString());
                     }
                 }
                 else
                 {
                     return;
                 }想做个 DataGridView 单元格里的验证功能 调试时在上段红色显示那行 提示 “未将对象引用设置到对象的实例” 弄不明白了 请大家赐教

解决方案 »

  1.   

    1)应判断:dataGridView1.Rows[rowIndex].Cells[0].Value != null
    2)string.IsNullOrEmpty(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());
      

  2.   

    没发好帖子 是上面那段的这行XMforT = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
      

  3.   

    可能是没有数据在里面 会报没有事例对象,你断点跟踪看dataGridView1.Rows[rowIndex].Cells[1].Value的值是多少?在.ToString()的时候可能报错;
      

  4.   

    你跟踪一哈,看哈那行里的rowIndex这个值是多少的情况下提示没有实例化对象.
    或者想1楼那样,加个判断.
      

  5.   

    dataGridView1.Rows[rowIndex].Cells[1].Value没有这一单元格.
      

  6.   

    噢 谢谢 大家 确实是 dataGridView1.Rows[rowIndex].Cells[1].Value 出现问题了可能用的事件不对