首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Gridview checkbox获取索引有关问题

2012-07-28 
Gridview checkbox获取索引问题protected void Button1_Click(object sender, EventArgs e){SqlConnection

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],有吗?估计是越界了

热点排行