这个是我原来做全选并删除时写的代码,希望对楼主有用
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1.Text == "全部选中")
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)row.Cells[0].FindControl("CheckBox1");
CheckBox1.Checked = true;
}
Button1.Text = "全部不选";
}
else
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)row.Cells[0].FindControl("CheckBox1");
CheckBox1.Checked = false;
}
Button1.Text = "全部选中";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)row.Cells[0].FindControl("CheckBox1");
if (CheckBox1.Checked == true)
{
SqlConnection con = DB.createCon();
SqlCommand cmd = new SqlCommand("delete from customer where customer_id='" + row.Cells[0].Text + "'", con);
con.Open();
cmd.ExecuteNonQuery();
GridView1.DataBind();
con.Close();
}
}
}