日期:2014-05-17  浏览次数:20376 次

Gridview checkbox获取索引问题
protected void Button1_Click(object sender, EventArgs e)
  {
  SqlConnection conn = new SqlConnection(DBHelper.connString);
  for (int i = 0; i <GridView1.Rows.Count; i++)
  {
  CheckBox ch = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
  if (ch.Checked == true)
  {
  string sql = "delete from carsinfo where carid='"+GridView1.DataKeys[i].Value+"'";
  SqlCommand command = new SqlCommand(sql,conn);
  conn.Open();
  command.ExecuteNonQuery();
  conn.Close();
  }
  }
  }

红色字体地方总是提示“索引超出范围。必须为非负值并小于集合大小
参数名: index”
不知道如何处理了

------解决方案--------------------
C# code

GridView1.Rows[i].Cells[carid所在列].Text

------解决方案--------------------
DataKeys[i],有吗?估计是越界了