gridview 一次删除多条记录,在gridview最左边加复选框,然后选中删除记录!
用自定义控件写,如何写??

解决方案 »

  1.   

        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);        con.Open();
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                if (((CheckBox)this.GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
                {
                    int id = Convert.ToInt32(this.GridView1.DataKeys[i][0].ToString());
                  //主键
    //删除语句自己写.....................
                }
            }
      

  2.   

    foreach (GridViewRow row in GridView1.Rows)
      {
        bool IsDel = ((CheckBox)row .Cells[x].Controls[0]).Checked  
        if (IsDel ){
             删除记录 //知道row删除该行数据很容易的.
        }
    }
      

  3.   

    請參考下面這段代碼           string selectedKeys="";    //保存所有选中的记录的主键
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridViewRow row = GridView1.Rows[i];
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        //检查是否选中,假设CheckBox的ID为Checkbox1,且在GridView1的第一列中
                        CheckBox cb = (CheckBox)row.Cells[0].FindControl("Checkbox1");
                        //如果选中,则记录选中记录的主键
                        if (cb.Checked) selectedKeys = selectedKeys + GridView1.DataKeys[row.RowIndex].Value.ToString() +",";
                    }
                }
                if (selectedKeys != "")
                {
                    selectedKeys=selectedKeys.TrimEnd(new char[] { ',' });
                    //生成sql语句,并执行删除
                    string sql = "delete from tablename where keycolumn in (" + selectedKeys + ")";
                    SqlConnection conn = new SqlConnection("连接字符串");
                    SqlCommand cmd = new SqlCommand(sql);
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                else
                {
                    Response.Write("未选中要删除的记录!");
                }
      

  4.   

    我以前是这样做的用一相模板列  里面放个复选框回复到服务器的时候看复杂框是不是被选中了判定这个我是这么做的 
    GridView g =this.你的gridview
            g.SelectedIndex = 0;
            g.SelectedRow.Cells
            g++;
    for(int i=0;i<g.Rows.Count;i++){
     CheckBox c=(CheckBox)g.SelectRows.Cells[序列].FindControl(控件ID);
      if(c.Select==true){
        你的操作
       }
    }反正上面这样可以达到效果,应该有更好的方法
    }GridView.
      

  5.   

    GridView g =this.你的gridview 
            g.SelectedIndex = 0; 
            g.SelectedRow.Cells 
            g++; 
    for(int i=0;i <g.Rows.Count;i++){ 
     CheckBox c=(CheckBox)g.SelectRows.Cells[序列].FindControl(控件ID); 
      g.SelectedIndex=i
      if(c.Select==true){ 
        你的操作 
       } 

      

  6.   

    客户端代码:
    绑定列
    <input type="checkbox" name="delItems" value='<%#Eval("ID")%>' id='check<%#Eval("ID")%>'/>后台:
    string delItems = Request.Form["delItems"];
    if(delItems != null && delItems.Length > 0){
       string sql = "delete from test where id in (" + delItems + ")";
    }或者string delItems = Request.Form["delItems"];
    foreach(string id in delItems.Split(',')){
     string sql = "delete from test where id = '" + id + "'";}
      

  7.   

    在你自定义控件的事件里:
    for(int i=0;i<gridview1.Items.Count;i++)
    {
     CheckBox newCheckBox=(CheckBox)gridview1.Items[i].FindControl("CheckBox1");//当前行的复选框
     if(newCheckBos.Checked)//如果该行被选中
     {
      //在此取得选中的记录行的索引号,也就是变量i的值,你可以把它保存到一个数组什么的,留着呆会用.
     }
    }for(int j=0;j<数组长度;j++)//取得选中行的关键值
    {
      GridView1.DataKeys[j].Value.ToString();//这就是你选中了复选框的记录的主键值,至于你要怎么删除,那是你的 
                                                   //事了,呵呵.多行删除,SQL语句要很长的哦,当然,这只是我的想法,
                                             //高手有优化算法,那就不同了
    }//以上代码有很多错的,因为我一般是用VS的智能感知来输入的.
    //上面只是提供一种参考而已
      

  8.   

    多种解决方法参见
    http://dotnet.aspx.cc/article/a8efc285-f0b1-4f8f-8e73-2b7d8724a47c/read.aspx
      

  9.   

    那个复选框有全选,如何添加脚本
    function SetAll(objChkAll)
    {
    var checkbox = document.all.tags("input");
                            //alert(checkbox.length);
    //alert(objChkAll.checked);
    for(var i=0;i<checkbox.length;i++)
    {
    //alert(checkbox[i].type);
                        //alert(checkbox[i].id);
    if((checkbox[i].type=="checkbox"))
    {
     checkbox[i].checked = objChkAll.checked;
    }
    }

    }