转自: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. ECMAScript 6 中的一些新特性

    1.箭头函数,直接写出来v =>看不出来什么,但是跟传统写法一比较,很直观地就能看出v =>是代替了匿名函数 function(v)的写法,{}与逻辑照旧,但是要注意,=与>之间不能 ...

  2. sql server经典sql

    1. sql server构造oracle rownum列 select * from ( select row_number() over(order by t.修改时间 desc) rownum, ...

  3. JS数值输入控制

    在html文本框录入数值时,可用如下方法进行控制判断. 整数:<input type="text" name="aaa" onkeypress=" ...

  4. OpenCV(4)-图像掩码操作(卷积)--平滑处理

    卷积定义 矩阵的掩码操作即对图像进行卷积.对图像卷积操作的意义为:邻近像素对(包括该像素自身)对新像素的影响:影响大小取决于卷积核对应位置值得大小. 例如:图像增强可以使用 \[ I(i,j)=5*I ...

  5. starling 中的 EventDispatcher 和 Flash中原生的 EventDispatcher

    starling 比较早之前就有开始解了,但只到最近参与一个用starling 做为框架的手游项目才真正做为一程来使用它. 项目也是刚开始搭建,在这做些笔记. 在写一个管理类时, 遇到 starlin ...

  6. fstat().stat()函数

    int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); 唯一不同是参数不同,其他一样. 文 ...

  7. java String字符串——进度1

    String字符串    在JAVA中提供了多种创建字符串对象的方法,这里介绍最简单的两种,    第一种是直接赋值,    第二种是使用String类的构造方法:    如下所示:    Strin ...

  8. 数组-去重、排序方法、json排序

    1.数组去重 /*方法一: 1,'1' 会被认为是相同的; 所有hash对象,如:{x;1},{y:1}会被认为是相同的 //10ms */ Array.prototype.unique=functi ...

  9. 前端工程的构建工具对比 Gulp vs Grunt

    1. Grunt -> Gulp 早些年提到构建工具,难免会让人联想到历史比较悠久的Make,Ant,以及后来为了更方便的构建结构类似的Java项目而出现的Maven.Node催生了一批自动化工 ...

  10. 【WPF】Dispatcher及线程操作

    WPF 应用程序启动后,会有两个线程: 1. 一个是用来处理UI呈现(处理UI的请求,比如输入和展现等操作). 2. 一个用来管理 UI的 (对UI元素及整个UI进行管理). 像Winform一样,W ...