索引超出范围。必须为非负值并小于集合大小。
List<string> list1 = new List<string>();//定义一个list行的队列for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[11].FindControl("CB1");//将GRIDVIEW中的每行的按钮的ID参数赋给 cb
if (cb1.Checked == true)
{
list1.Add(GridView1.DataKeys[i].Value.ToString());//将选中的行记录到LIST中
}
}
if (list1.Count <= 0)//没有选中的按钮
{
Response.Write("<script language='javascript'>alert('请选择要删除的项!')</script>");
return;
}
string usernos = String.Join(",", list1.ToArray());
try
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]))
{
conn.Open();
string DeleteCmd = string.Format("delete from suppliers where supplyersid in ({0})", usernos);
using (SqlCommand MyCommand = new SqlCommand(DeleteCmd, conn))
{
MyCommand.ExecuteNonQuery();
}
conn.Close();
}
GridView1.DataBind();
Response.Write("<script language='javascript'>alert('成功删除!')</script>");
}
catch
{
Response.Write("<script>alert('删除失败!');history.back();</Script>");
}
高手帮帮给看看这段代码运行了之后在删除的时候,
系统报错说:
list1.Add(GridView1.DataKeys[i].Value.ToString());
索引超出范围。必须为非负值并小于集合大小。