girdview中的数据是绑定的从数据库中读取的,如何在gridview中加一列checkbox复选框,再加一个删除按钮,点击删除选中的数据?

解决方案 »

  1.   

    模版列加个CheckBox 
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox2" runat="server" />
                        </ItemTemplate>
    后台
     protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("CheckBox2");
                    if (cb.Checked==true)
                    {
                        删除代码
                    }
                }
            }
      

  2.   

    protected void btnDel_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow i in GridView1.Rows)//也可以循环Table
            {
                CheckBox check = (CheckBox)i.FindControl("Mycheck");
                if (check.Checked)
                {
                    int id = Convert.ToInt32(i.Cells[1].Text);
                    GetDel(id);
                    GetDataBind();
                    RegisterStartupScript("", "<script>alert('删除成功')</script>");
                }
            }
           // DelBook();
        }
     public void GetDel(int i)
        {
            string mysql = "delete from Book where BID=@BID";
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["MyConn"]);
            conn.Open();
            SqlCommand cmd = new SqlCommand(mysql, conn);
            cmd.Parameters.Add("@BID", SqlDbType.Int);
            cmd.Parameters["@BID"].Value = i;
            cmd.ExecuteNonQuery();
        }
      

  3.   

    我能得到选中了哪个checkbox,可是在删除代码中如何得到checkbox这一行的唯一标识呢?
      

  4.   

    先设置DataKeyNames=你的字段名
    取值string userid = GridView1.DataKeyNames.DataKeys[e.RowIndex].Value.ToString();
      

  5.   

    楼上的,谢谢,不过还不行我这样怎么提示编译错误 我设计了DataKeyNames为我数据库的标识id字段, 编译器错误信息: CS0117: “System.Array”并不包含“DataKeys”的定义foreach (GridViewRow row in dgList.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("cbID");
                if (cb.Checked == true)
                {
                    strID = strID + dgList.DataKeyNames.DataKeys[row.RowIndex].Value.ToString();
                }
            }
            Response.Write(strID);
      

  6.   

    protected void btnDel_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow i in GridView1.Rows)//也可以循环Table
            {
                CheckBox check = (CheckBox)i.FindControl("Mycheck");
                if (check.Checked)
                {
                    int id = Convert.ToInt32(i.Cells[1].Text);
                    GetDel(id);
                    GetDataBind();
                    RegisterStartupScript("", "<script>alert('删除成功')</script>");
                }
            }
           // DelBook();
        }
    按照这里id的取法,可以取到你所要的主键的数据啊。
      

  7.   

    不好意思,上面贴错了
    string userid = GridView1.DataKeys[e.RowIndex].Value.ToString();