当前位置: 代码迷 >> ASP.NET >> Gridview checkbox获取索引有关问题
  详细解决方案

Gridview checkbox获取索引有关问题

热度:9093   发布时间:2013-02-25 00:00:00.0
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],有吗?估计是越界了
  相关解决方案