日期:2014-05-18  浏览次数:20720 次

请问:datatable某字段的返回值出错
DataTable userdt = new DataTable();
 BindingManagerBase mybind;
  private void modifyUser_Load(object sender, EventArgs e)
  {
  userdt = getUserinfo("select * from users");//自定义函数,返回所有纪录
  DataGridview1.DataSource = userdt;
  mybind = BindingContext[userdt];
  }

  private void btn_del_Click(object sender, EventArgs e)
  {
  int IDPosition = this.mybind.Position;
  if (IDPosition != -1)
  { 
  //删除数据库中的行
  string userId = userdt.Rows[IDPosition]["user_Id"].ToString(); //error
  userUpdate("delete from user where user_Id =" + int.Parse(userId));
  }
  }

在error出错误提示"在位置2处没有任何行",但我已经选中了一行啊,"user_Id"是数据库表中的字段。请问哪里的错误??

------解决方案--------------------
你加个注释点,跟踪一下。看看 string userId = userdt.Rows[IDPosition]["user_Id"].ToString(); 中的userdt是不是没有第二行。
------解决方案--------------------
IDPosition 的问题 按错误提示应该是这个值为2 但是userdt的行数应该是<=2的 所以出这个错误
你看看IDPosition 是不是需要-1