首页 > 代码库 > ASP.NET(C#) GridView (编辑、删除、更新、取消)

ASP.NET(C#) GridView (编辑、删除、更新、取消)

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

前台代码

 

view source
 
print?
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

 

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