日期:2014-05-18  浏览次数:20902 次

这段代码为什么只能删除一行呢?
C# code

 protected void allDelDropDownList()
    {

        string rowsID = "";
        for (int i = 0; i < gV_ArticleCotrol.Rows.Count; i++)
        {
            CheckBox chbox = (CheckBox)gV_ArticleCotrol.Rows[i].FindControl("checkBoxX");
            if (chbox.Checked == true)
            {
                rowsID = rowsID + gV_ArticleCotrol.DataKeys[i].Value.ToString();//获取选中ID

                string delStr = "delete from articleControl_Table where artid='" + rowsID + "' ";
                SqlConnection sqlDel = connectSqlData();
                sqlDel.Open();
                SqlCommand delCommand = new SqlCommand(delStr, sqlDel);
                delCommand.ExecuteNonQuery();
                sqlDel.Close();
                gV_ArticleCotrol.EditIndex = -1;
                gV_ArticleCotrol.DataSource = SqlDataArtSouce;
                gV_ArticleCotrol.DataBind();  
            }
            
        }

        gv_DataBind(); 
    }


如题,为什么只能删除ID值最小的哪一项呢?求解,问题出现在那里呢??

------解决方案--------------------
调试一下, 感觉你在循环中DataBind有点问题
------解决方案--------------------
1.gV_ArticleCotrol.EditIndex = -1;
造成的吧

2.不喜欢这样的批量处理方法
------解决方案--------------------
应该将字段定义放在循环里面,否则一直累加,只有最小的时候才是正确的rowsID 
C# code

string rowsID = "";

------解决方案--------------------
rowsID = rowsID + gV_ArticleCotrol.DataKeys[i].Value.ToString();//获取选中ID

为啥要不断的加呢。。。
------解决方案--------------------
rowsID = rowsID + gV_ArticleCotrol.DataKeys[i].Value.ToString();//获取选中ID

rowsID怎么会循环递加呢??

rowsID = gV_ArticleCotrol.DataKeys[i].Value.ToString();//获取选中ID
------解决方案--------------------
string rowsID = "";
for (int i = 0; i < gV_ArticleCotrol.Rows.Count; i++)
{
CheckBox chbox = (CheckBox)gV_ArticleCotrol.Rows[i].FindControl("checkBoxX");
if (chbox.Checked == true)
{
rowsID = rowsID +","+ gV_ArticleCotrol.DataKeys[i].Value.ToString();//获取选中ID

}

}
string delStr = "delete from articleControl_Table where artid in('" + rowsID + "' )";
SqlConnection sqlDel = connectSqlData();
sqlDel.Open();
SqlCommand delCommand = new SqlCommand(delStr, sqlDel);
delCommand.ExecuteNonQuery();
sqlDel.Close();
gV_ArticleCotrol.EditIndex = -1;
gV_ArticleCotrol.DataSource = SqlDataArtSouce;
gV_ArticleCotrol.DataBind();