使用复选框删除gridview中的数据库数据 [英] Delete the database data in gridview using checkbox
本文介绍了使用复选框删除gridview中的数据库数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我有一个带有复选框的gridview和一个按钮来从数据库中删除选定的行.即时消息告诉我正在从数据库中删除选定的项目(例如:gmail邮件删除选项).请告诉我如何逐步执行,这意味着它对我的帮助比thanku.
in my project i have one gridview with checkbox and one button to delete the selected row from database.What im telling is deleteting selected items from database(ex:gmail mail delete option).i tried some codes in google but its not working if u plz tell how to do step by step means its help full for me thanku.
推荐答案
试试这个
Hi ,
Try this
<div>
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
DataKeyNames="id" DataSourceID="SqlDataSource1"
onrowdeleting="GridView1_RowDeleting">
<columns>
<asp:boundfield datafield="id" headertext="id" insertvisible="False">
ReadOnly="True" SortExpression="id" />
<asp:boundfield datafield="cate" headertext="cate" sortexpression="cate" />
<asp:templatefield>
<itemtemplate>
<asp:checkbox id="CheckBox2" runat="server">
oncheckedchanged="CheckBox2_CheckedChanged" />
</asp:checkbox></itemtemplate>
</asp:templatefield>
<asp:commandfield showdeletebutton="True" />
</asp:boundfield></columns>
</asp:gridview>
<asp:sqldatasource id="SqlDataSource1" runat="server" xmlns:asp="#unknown">
ConnectionString="<%
ConnectionStrings:testConnectionString < /asp:sqldatasource > < br > < asp:button id =" runat 服务器" onclick Button1_Click" 文本 按钮" xmlns:asp =" #unknown" > style ="height:26px"/> < /asp:button > < /div >
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT * FROM [cate]"></asp:sqldatasource> <br /> <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" xmlns:asp="#unknown"> style="height: 26px" /> </asp:button></div>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox2")) is CheckBox)
{
if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox2")).Checked == true)
{
//delete from DB
}
}
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked == true )
{
//Do Somthing
}
else if (((CheckBox)sender).Checked == false)
{
//do something else
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if ((CheckBox)row.FindControl("CheckBox2") is CheckBox)
{
if (((CheckBox)row.FindControl("CheckBox2")).Checked == true)
{
//Do Somthing
}
else if (((CheckBox)row.FindControl("CheckBox2")).Checked == false)
{
//do something else
}
}
}
}
最好的问候
M.Mitwalli
Best Regards
M.Mitwalli
请参阅以下文章:
http://www.aspdotnet-suresh.com/2011/02/how-to-delete-multiple-rows-in-gridview.html [ http://geekswithblogs.net/dotNETvinz/存档/2009/02/22/gridview-multiple-delete-with-checkbox-and-confirm.aspx [
Refer to these articles:
http://www.aspdotnet-suresh.com/2011/02/how-to-delete-multiple-rows-in-gridview.html[^]
http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-multiple-delete-with-checkbox-and-confirm.aspx[^]
这篇关于使用复选框删除gridview中的数据库数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文