foreach (GridViewRow gv in GridView3 .Rows)
{
CheckBox cBox =
(CheckBox) gv.FindControl ("CheckBox1");
if (cBox .Checked == true)
{
string type = Request. QueryString["type1" ].ToString();
string id = GridView3. DataKeys[gv .RowIndex]. Value.ToString ();
string sql = string. Format("delete
from 用户表 where sn = '{0}'" , id);
int num = Sqlhelper. ExecInsert_Update_Delete(sql );
if (num
!= 0)
{
string strsql = string. Format("Select
* from 用户表 where 用户类型='{0}'" ,type);
DataTable dt = Sqlhelper. GetDataTable(strsql );
GridView3. DataSource = dt ;
GridView3. DataBind();
}
else
{
//失败
}
}
}
错误原因:每删完一行都会刷新页面,从而导致下一条要删除的数据已经往上移了,刷新完再去删除就找不到要删除的数据了,这就是导致删除的时候会隔行删除的原因.比如说我们要删除第1,2,3条数据,当删完第1条刷新页的时候,第2条数据往上移到第1条数据的位置了,第3条数据移到第2条的位置了,现在执行删除第2条数据的时候就会删到第3条,再想删除第3条的时候已经没有数据可删除,就会报错:
解决办法:全部删除完再刷新页面
int num =
0;
foreach (GridViewRow gv in GridView3 .Rows)
{
CheckBox cBox =
(CheckBox) gv.FindControl ("CheckBox1");
if (cBox .Checked == true)
{
string id = GridView3. DataKeys[gv .RowIndex]. Value.ToString ();
string sql = string. Format("delete
from 用户表 where sn = '{0}'" , id);
num = Sqlhelper.ExecInsert_Update_Delete (sql);
}
}
if (num
!= 0)
{
string type =
Request.QueryString[ "type1"].ToString();
string strsql = string. Format("Select
* from 用户表 where 用户类型='{0}'" , type);
DataTable dt = Sqlhelper. GetDataTable(strsql );
GridView3. DataSource = dt ;
GridView3. DataBind();
}
else
{
//失败
}