一楼正解。
for(int i=0;i
if(((CheckBox)gv.Rows[i].Cells[0].Controls[1]).Checked)
{
//删除代码
}
}
//Cells里面的Controls[0]是Literal,Controls[1]才是CheckBox
public partial class GridiewCheckBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
///
/// 绑定数据
///
public void bind()
{
string sqlStr = "select * from Employee";
DataSet myds = Common.dataSet(sqlStr);
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
}
///
/// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
///
///
///
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
}
///
/// 当复选框被点击时发生
///
///
///
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 Button1_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)
{
Common.ExecuteSql("delete from Employee where ID=" + Convert.ToUInt32(GridView1.DataKeys[i].Value) + "");
}
}
bind();