for (int i = 0; i < exdgLog.Items.Count; i++)
        {            CheckBox cb = exdgLog.Items[i].FindControl("cbxLogData ") as CheckBox;
            if (cb.Checked == true)
            {
                string strSQLDelete = "DELETE FROM LOGINFO LOG_ID = '" +  + "'";
                DBHelper.ExecSQL(strSQLDelete);
            }
        }
我需要删除checkbox选中的值 。LOG_ID(日志表主键)应该等于什么呢?        foreach (DataGridItem i in exdgLog.Items)
        {
           
            if (((CheckBox)i.FindControl("cbxLogData")).Checked == true)
            {
                //记录选中的日志ID
                   string  strDelSql = "DELETE FROM LOGINFO WHERE  LOG_ID='" + i.Cells[1].Text + "'";
                   DBHelper.ExecSQL(strDelSql);
                   BindData();
            }
        }用这种方法我可以得到checkbox的text  但是报错invalidOperationException(集合已修改;可能无法执行枚举操作)。
帮帮我 !  解决尽量详细点   我得学会了。

解决方案 »

  1.   

    foreach操作完以后在重新绑定数据
      

  2.   

    不用foreach,改用for循环。
    另外,你为什么不组合一个
    delete from loginfo where log_id in (id1,id2,id3....)这样一个字符串一次性删掉所有选中的行?
      

  3.   


    如果等于false  那你的意思就是反选啦,选中的不删?
      

  4.   

    foreach (DataGridItem i in exdgLog.Items)
    你对集合exdgLog.Item进行操作了以后集合就被改变了,foreach无法进行这样的操作,改为for循环即可
      

  5.   


     我现在是日志的id不知怎么弄成等于checkbox选中的那行的id
      

  6.   


    string strSQLDelete = "DELETE FROM LOGINFO LOG_ID = '" +  + "'";用for我不知道怎么把id和选中的id做匹配
      

  7.   

    foreach (DataGridItem i in exdgLog.Items)
      {
        
      if (((CheckBox)i.FindControl("cbxLogData")).Checked == true)
      {
      //记录选中的日志ID
      string strDelSql = "DELETE FROM LOGINFO WHERE LOG_ID='" + i.Cells[1].Text + "'";
      DBHelper.ExecSQL(strDelSql);
      }
      }  
    BindData();......直接放外边
    或者for(int i=0;i<gv.rows.Count;i++)循环,当执行删除时i--
      

  8.   


    用for 如何匹配表id是否就是checkbox选中的id
      

  9.   

    for(int i=0;i<gv.rows.Count;i++)
    {
    string id=gv.rows[i].cell[1].tostring();
    if(删除)
    {
    i--;
    }
    }
    大概就这样,手打的,不对就自己找下吧
      

  10.   

    多线程环境下,其他线程修改了集合
    另外一个线程对集合进行枚举操作,读取脏数据
    for循环操作
      

  11.   

    LZ如果要用foreach也可以string ids="";
      foreach (DataGridItem i in exdgLog.Items)
      {
        
      if (((CheckBox)i.FindControl("cbxLogData")).Checked == true)
      {
      int count=0;
    if(count==0)
    {
    ids=i.Cells[1].Text ;count++;
    }esle{
      ids+=","+ i.Cells[1].Text ;
    }
      
      }
      }public void method(string ids)
    {
    string [] id=ids.split(',');
    for(int i=0;i<id.length;i++)
    {
    string strDelSql = "DELETE FROM LOGINFO WHERE LOG_ID='" + id[i]+ "'";
     DBHelper.ExecSQL(strDelSql);
    }
      BindData();
    }