详解GridView自带的编辑删除更新功能
网络编程 2021-07-04 22:41www.168986.cn编程入门
本文主要介绍了GridView自带的编辑、删除、更新功能实现的方法。具有一定的参考价值,需要的朋友一起来看下吧
GridView自带编辑删除更新逻辑很简单操作完,重新绑定。,防止忘记。。。
效果图
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridView_bianjidelete.aspx.cs" Inherits="gridView_bianjidelete" %> <!DOCTYPE html> <html xmlns="http://.w3./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./21asp SqlConnection sqlcon; SqlCommand sql; 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); sql = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); sql.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() + "'"; sql = new SqlCommand(sqlstr, sqlcon); sqlcon.Open(); sql.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(); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望多多支持狼蚁SEO!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程