gridview删除行出问题啦(在线急等,解决立马给分)
大家帮忙看看咯,gridview删除行出问题啦。。。先谢谢啦
索引超出范围。必须为非负值并小于集合大小。
参数名: index
SqlConnection con = new SqlConnection( "data source=QW;uid=sa;pwd=517123;database=wuqiang ");
for (int i = 0; i <= grid.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)grid.Rows[i].Cells[0].FindControl( "chk ");
if (cbox.Checked)
{
SqlCommand cmd = new SqlCommand( "delete from login where loginName= ' " + grid.DataKeys[i].Value.ToString() + " ' ", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
string id = grid.DataKeys[1].Value.ToString();
}
}
[解决办法]
有没有指定DataKeyField?
[解决办法]
GridView设置DataKeyNames属性
<asp:GridView ID= "GridView1 " runat= "server " DataKeyNames= "loginName " ...