怎么点击页面上的删除按钮,执行Griview中每行复选框选中项的删除操作?

解决方案 »

  1.   

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string rightCode="";        foreach (GridViewRow gvr in this.GridView1.Rows)
            {
                CheckBox chk = (CheckBox)gvr.FindControl("CheckBox1");
               
                
                if (chk.Checked)
                {
                    
                    //删除
                }
            }
       }hehe
      

  2.   

    删除哪里当然要获得GridView1.DataKeys[gvr.RowIndex].Value.ToString()
      

  3.   

       protected void btnDelete_Click(object sender, EventArgs e)
            {
                List<int> lst = new List<int>();
                foreach (GridViewRow gr in this.gvNews.Rows)
                {
                    if (((CheckBox)gr.FindControl("CheckBox1")).Checked)
                        lst.Add(int.Parse(((HiddenField)gr.FindControl("Hd_Id")).Value));
                }
                foreach (int i in lst)
                {
                  //删除
                }
                BindData();//绑定数据
               
            }
      

  4.   

    这个 string rightCode="";
    有什么用?
    本人自学。见笑了
      

  5.   

    他可能是copy人家的代码!不用也成!!
      

  6.   

    前台<asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox ID="chkSelected" runat="server" Checked="False" />
        </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField HeaderText="用户ID">                           
        <ItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%# Eval("UserID") %>'></asp:Label>
            </ItemTemplate>
       </asp:TemplateField>
    后台: protected void ImageButtonDelete_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl("chkSelected")).Checked;
                if (isChecked)
                {
                    string userID = ((Label)GridView1.Rows[i].FindControl("Label1")).Text;
                    Users user = new Users();
                    user.DeleteByProc(userID)) //根据userID删除它            }
            }
            GridViewBind(); //重新绑定GridView
        }
      

  7.   

    Hd_Id中保存主键,根据主键删除
      

  8.   

    代码操作
    datagirdview删除数据
    在datagirdview的CLICK事件中写
    string  productID=this.datagirdview.SelectedCell[0].value.ToString();//返回第一列  根据id来删除    在之前要定义private  String productID
     在按钮的click事件中写
    DialogResult answer=MessageBox.Show("确定删除吗","删除操作",MessageBoxButtons.YesNo);
    if(answer == DialogResult .Yes)
    {
         int cuont=this.deleteProduct(productID);
        MessageBox.Show("删除行数:"+count.ToString());
    }
      private int deleteProduct(string id)
    {
        int count=0;
      //打开connection
      //定义一个SQL语句
     //定义一个command
     //用command对象执行语句
     //关闭connection
    string sql="delete from products where id =" +id;
    SqlCommand comm=new SqlCommand(sql.con);
    con.open();
    count=comm.ExecuteNonQuery();
    con.Close();
    return count; 
    }