GridView全选和批量删除

来源:互联网 发布:金山手机数据恢复大师 编辑:程序博客网 时间:2024/06/05 21:58

 //全选

    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)

    {

        for (int i = 0; i <= GridView1.Rows.Count-1;i++ )

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (CheckBox2.Checked == true)

            {

                cbox.Checked = true;

            }

            else

            {

                cbox.Checked = false;

            }

        }

    }

 

    //全部删除

    protected void btnAllDelete_Click(object sender, EventArgs e)

    {

        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)

        {

            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)

            {

                string sqlstr = "delete from tb_Member where MemberID='"+GridView1.DataKeys[i].Value+"'";

                SqlConnection myConn = GetConnection();

                SqlCommand myCmd = new SqlCommand(sqlstr, myConn);

                myConn.Open();

                myCmd.ExecuteNonQuery();

                myConn.Close();

            }

        }

        bind();

    }

 

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        CheckBox2.Checked = false;

        string sqlstr = "delete from tb_Member where MemberID=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

        SqlConnection myConn = GetConnection();

        SqlCommand myCmd = new SqlCommand(sqlstr, myConn);

        myConn.Open();

        myCmd.ExecuteNonQuery();

        myConn.Close();

        bind();

    }