girdview中的数据是绑定的从数据库中读取的,如何在gridview中加一列checkbox复选框,再加一个删除按钮,点击删除选中的数据?
解决方案 »
- c# 邮箱系统
- xml,SimpleContent 列问题
- 静态页中的datalist
- 父窗体textbox获取弹出窗体的值
- !!急需解决在WEB方式下的水晶报表打印问题(再线等,紧急)
- 文件下载的问题,对你来说应该不是很难
- asp.net MVC4 遍历Model的自定义IList属性,出现命名空间不存在类型或命名空间名称
- 有关水晶报表打印问题
- crystal中数值的尾零的问题
- 我现在在网页上用此语句<TD style="WIDTH: 62px; HEIGHT: 31px" align="middle" colSpan="6"><%# container.DataItem(20)%></TD>显示的是
- 用asp.net 动态显示出树 非常感谢!!!
- 求书---《ASP.NET数据库高级教程(C#篇)》
<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)
{
删除代码
}
}
}
{
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();
}
取值string userid = GridView1.DataKeyNames.DataKeys[e.RowIndex].Value.ToString();
{
CheckBox cb = (CheckBox)row.FindControl("cbID");
if (cb.Checked == true)
{
strID = strID + dgList.DataKeyNames.DataKeys[row.RowIndex].Value.ToString();
}
}
Response.Write(strID);
{
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的取法,可以取到你所要的主键的数据啊。
string userid = GridView1.DataKeys[e.RowIndex].Value.ToString();