转自:http://my.oschina.net/dldlhrmc/blog/93458

前台代码

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
02   
03  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04   
05  <html xmlns="http://www.w3.org/1999/xhtml">
06  <head runat="server">
07      <title></title>
08  </head>
09  <body>
10      <form id="form1" runat="server">
11      <div>
12       
13          <asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True"
14              AutoGenerateEditButton="True" DataKeyNames="id"
15              onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
16              onrowupdating="GridView1_RowUpdating"
17              onrowcancelingedit="GridView1_RowCancelingEdit" >
18          </asp:GridView>
19       
20      </div>
21      </form>
22  </body>
23  </html>

后台代码

01 using System;
02  using System.Collections.Generic;
03  using System.Linq;
04  using System.Web;
05  using System.Web.UI;
06  using System.Web.UI.WebControls;
07  using System.Data.SqlClient;
08  using System.Data;
09   
10  public partial class Default5 : System.Web.UI.Page
11  {
12      private void BindData()
13      {
14          String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True";
15          SqlConnection con = new SqlConnection(Strcon);
16          String sql = "select userinfo.id,username,password,birthday from userinfo";
17          SqlDataAdapter ad = new SqlDataAdapter(sql, con);
18          DataSet ds = new DataSet();
19          ad.Fill(ds);
20          GridView1.DataSource = ds;
21          GridView1.DataBind();
22      }
23   
24      protected void Page_Load(object sender, EventArgs e)
25      {
26          if (!IsPostBack)  /*如果省略这句,下面的更新操作将无法完成,因为获得的值是不变的*/
27          {
28              BindData();
29          }
30      }
31      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
32      {
33          String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True";
34          SqlConnection con = new SqlConnection(Strcon);
35          int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置 DataKeyNames,这里设为 id */
36          String sql = "delete from userinfo where id='" + id+"'";
37           
38          SqlCommand com = new SqlCommand(sql, con);
39          con.Open();
40          com.ExecuteNonQuery();
41          con.Close();
42          BindData();
43   
44      }
45   
46      /*编辑操作,利用e.NewEditIndex获取当前编辑行索引*/
47      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
48      {
49          GridView1.EditIndex = e.NewEditIndex;
50          BindData();              /*再次绑定显示编辑行的原数据,不进行绑定要点2次编辑才能跳到编辑状态*/
51      }
52      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
53      {
54          String Strcon = "Data Source=.;Initial Catalog=testDB;Integrated Security=True";
55          SqlConnection con = new SqlConnection(Strcon);
56          String username = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text.ToString();    /*获取要更新的数据*/
57          String password = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text.ToString();
58          String birthday = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString();
59   
60          int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*获取主键,需要设置 DataKeyNames,这里设为 id */
61   
62          String sql = "update userinfo set username='" + username + "',password='"+password+"',birthday='"+birthday+"' where id='"+id+"'";
63   
64          SqlCommand com = new SqlCommand(sql, con);
65          con.Open();
66          com.ExecuteNonQuery();
67          con.Close();
68          GridView1.EditIndex = -1;
69          BindData();
70      }
71      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
72      {
73          GridView1.EditIndex = -1;                 /*编辑索引赋值为-1,变回正常显示状态*/
74          BindData();
75      }
76  }

SQL 脚本(SQL Server)

01 USE [testDB]
02  GO
03   
04  /****** Object:  Table [dbo].[userinfo]    Script Date: 12/02/2012 22:20:46 ******/
05  SET ANSI_NULLS ON
06  GO
07   
08  SET QUOTED_IDENTIFIER ON
09  GO
10   
11  SET ANSI_PADDING ON
12  GO
13   
14  CREATE TABLE [dbo].[userinfo](
15     [id] [int] IDENTITY(1,1) NOT NULL,
16     [username] [varchar](50) NULL,
17     [password] [varchar](50) NULL,
18     [birthday] [varchar](50) NULL,
19   CONSTRAINT [PK_userinfo] PRIMARY KEY CLUSTERED
20  (
21     [id] ASC
22  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]
23  ON [PRIMARY]
24   
25  GO
26   
27  SET ANSI_PADDING OFF
28  GO

本人菜鸟,勿喷!仅供参考。

ASP.NET(C#) GridView (编辑、删除、更新、取消)的更多相关文章

  1. GridView总结二:GridView自带编辑删除更新

    GridView自带编辑删除更新逻辑很简单:操作完,重新绑定.总结总结,防止忘记... 效果图: 前台代码: <%@ Page Language="C#" AutoEvent ...

  2. GridView编辑删除操作

    第一种:使用DataSource数据源中自带的编辑删除方法,这样的不经常使用,在这里就不加说明了. 另外一种:使用GridView的三种事件:GridView1_RowEditing(编辑).Grid ...

  3. GridView编辑删除

    A前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...

  4. GridView 编辑,更新,删除 等操作~~

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1. ...

  5. Asp.Net:GridView 编辑、删除、自定义分页以后备用

    页面 GridView 绑定:在中,有 <asp:BoundField/>和 <asp:TemplateField><ItemTemplate>嵌套服务器控件 &l ...

  6. ASP.NET编辑与更新数据(非GridView控件实现)

    Insus.NET在实现<ASP.NET开发,从二层至三层,至面向对象 (5)>http://www.cnblogs.com/insus/p/3880606.html 中,没有把数据编辑与 ...

  7. gridview的编辑,更新,取消,自动分页等

    gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉 添加boundfield(绑定列)将其datafield设置为productname,headertext设置为 ...

  8. GridView 编辑、删除 、分页

    类似代码都差不多,记录一下,便于以后查看使用. 前台页面: <asp:GridView ID="gdvList" runat="server" AutoG ...

  9. asp gridview批量删除和全选

    本人新手刚学asp.net   全选和删除也是引用了他人的代码试了一试可以实现,感觉很好,就发了上来. 前台代码 <asp:GridView ID="GridView1" r ...

  10. GridView编辑、取消按钮自定义控件

    这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. < ...

随机推荐

  1. 利用Highcharts制作web图表学习(二)

        最近中海油的项目需要用到图表展示数据,最近还是一直边学习边开发,今天做了一个展示,炼化厂加热炉效率展示的柱状图,把代码贴出来,大家指点一下互相学习,我是通过数组给Highcharts绑定的值, ...

  2. javascript dom编程艺术笔记第三章:DOM操作的5个基本方法

    JavaScript的 DOM操作,主要是对DOM这三个字母中D.O.M的操作.D代表的是document(文档),即我们可以使用javascript对文档进行操作,O代表的是object(对象),对 ...

  3. javax.el.PropertyNotFoundException: Property 'aDesc' not found on type

    这个问题是是在我使用jeesite自动代码是产生的,原因是实体类的属性命名规范不合格,我在网上看到类的属性前三个字母不能出现大写 解决办法:将类的属性大小写改一下

  4. notepad++插件使用说明

    在进行渗透测试的过程中,经常需要分析一些js或xml文件,或者是一些url,它们多是经过压缩或编码的,看起来不方便.这里介绍几款常用的notepad++插件,会极大的方便我们的开发测试. 1 Xml ...

  5. [C#]『CountdownEvent』任务并行库使用小计

    System.Threading.CountdownEvent  是一个同步基元,它在收到一定次数的信号之后,将会解除对其等待线程的锁定. CountdownEvent  专门用于以下情况:您必须使用 ...

  6. Javascript自执行匿名函数(function() { })()的原理浅析

    匿名函数就是没有函数名的函数.这篇文章主要介绍了Javascript自执行匿名函数(function() { })()的原理浅析的相关资料,需要的朋友可以参考下 函数是JavaScript中最灵活的一 ...

  7. python 数据运算

    算数运算:  

  8. List<>.Contains<>的用法

    List<Plan> Plans = new List<Plan>();//存放服务器中的当前用户所接受的项目计划列表. //Plan 类包含PlanID等属性. if (Pl ...

  9. C#中指针使用总结

    C#为了类型安全,默认并不支持指针.但是也并不是说C#不支持指针,我们可以使用unsafe关键词,开启不安全代码(unsafe code)开发模式.在不安全模式下,我们可以直接操作内存,这样就可以使用 ...

  10. bzoj3677: [Apio2014]连珠线

    Description 在列奥纳多·达·芬奇时期,有一个流行的童年游戏,叫做“连珠线”.不出所料,玩这个游戏只需要珠子和线,珠子从1到礼编号,线分为红色和蓝色.游戏 开始时,只有1个珠子,而接下来新的 ...