读书人

Gridview checkbox获取索引有关问题

发布时间: 2012-07-28 12:25:13 作者: rapoo

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

读书人网 >asp.net

热点推荐