详解GridView自带的编辑删除更新功能
程序员文章站
2022-08-27 17:04:50
gridview自带编辑删除更新逻辑很简单:操作完,重新绑定。总结总结,防止忘记。。。
效果图:
前台代码:
<%@ page language...
gridview自带编辑删除更新逻辑很简单:操作完,重新绑定。总结总结,防止忘记。。。
效果图:
前台代码:
<%@ page language="c#" autoeventwireup="true" codefile="gridview_bianjidelete.aspx.cs" inherits="gridview_bianjidelete" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" cellpadding="4" forecolor="#333333" gridlines="none" onrowdeleting="gridview1_rowdeleting" onrowediting="gridview1_rowediting" onrowupdating="gridview1_rowupdating" onrowcancelingedit="gridview1_rowcancelingedit"> <footerstyle backcolor="#990000" font-bold="true" forecolor="white" /> <columns> <asp:boundfield datafield="id" headertext="产品id" readonly="true" /> <asp:boundfield datafield="name" headertext="产品name" /> <asp:boundfield datafield="stock" headertext="库存" /> <asp:commandfield headertext="选择" showselectbutton="true" /> <asp:commandfield headertext="编辑" showeditbutton="true" /> <asp:commandfield headertext="删除" showdeletebutton="true" /> </columns> <rowstyle forecolor="#000066" /> <selectedrowstyle backcolor="#669999" font-bold="true" forecolor="red" /> <pagerstyle backcolor="white" forecolor="#000066" horizontalalign="left" /> <headerstyle backcolor="#006699" font-bold="true" forecolor="white" /> </asp:gridview> </div> </form> </body> </html>
后台代码:
using system; using system.collections.generic; using system.configuration; using system.data; using system.data.sqlclient; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; public partial class gridview_bianjidelete : system.web.ui.page {//清清月儿http://blog.csdn.net/21aspnet sqlconnection sqlcon; sqlcommand sqlcom; string strcon = configurationmanager.connectionstrings["sqlconnectionstring"].connectionstring; protected void page_load(object sender, eventargs e) { if (!ispostback) { bind(); } } protected void gridview1_rowediting(object sender, gridviewediteventargs e) { gridview1.editindex = e.neweditindex; bind(); } //删除之后重新绑定 protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e) { string sqlstr = "delete from product where id='" + gridview1.datakeys[e.rowindex].value.tostring() + "'"; sqlcon = new sqlconnection(strcon); sqlcom = new sqlcommand(sqlstr, sqlcon); sqlcon.open(); sqlcom.executenonquery(); sqlcon.close(); gridview1.databind(); bind(); } //更新 protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e) { sqlcon = new sqlconnection(strcon); string sqlstr = "update product set name='" + ((textbox)(gridview1.rows[e.rowindex].cells[1].controls[0])).text.tostring().trim() + "',stock='" + ((textbox)(gridview1.rows[e.rowindex].cells[2].controls[0])).text.tostring().trim() + "' where id='" + gridview1.datakeys[e.rowindex].value.tostring() + "'"; sqlcom = new sqlcommand(sqlstr, sqlcon); sqlcon.open(); sqlcom.executenonquery(); sqlcon.close(); gridview1.editindex = -1; // gridview1.databind(); bind(); } //取消 protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e) { gridview1.editindex = -1; bind(); } //绑定 public void bind() { string sqlstr = "select * from product p,uuser u where p.userid=u.id"; sqlcon = new sqlconnection(strcon); sqldataadapter myda = new sqldataadapter(sqlstr, sqlcon); dataset myds = new dataset(); sqlcon.open(); myda.fill(myds, "datatable"); gridview1.datasource = myds; gridview1.datakeynames = new string[] { "id" };//主键 gridview1.databind(); sqlcon.close(); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: 用vbs实现虚拟主机和域名查循的脚本