GridView中選中,編輯,取消,刪除操作?1.CREATE TABLE [dbo].[Employee] ( [ID] [int] NOT NULL , [身份證號(hào)碼] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [姓名] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [員工性別] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL , [家庭住址] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [郵政編碼] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [出生日期] [smalldatetime] NULL , [起薪] [money] NULL ) ON [PRIMARY] 2.在頁(yè)面中創(chuàng)建一個(gè)GridView,添加表中有的數(shù)據(jù)列 3.前臺(tái)代碼 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd <html xmlns="http://www./1999/xhtml <head runat="server"> <title>無(wú)標(biāo)題頁(yè)</title> <link href="css.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" Width="544px" AutoGenerateColumns="False" > <Columns> <asp:BoundField DataField="身份證號(hào)碼" HeaderText="用戶(hù)ID" /> <asp:BoundField DataField="姓名" HeaderText="用戶(hù)姓名" /> <asp:BoundField DataField="員工性別" HeaderText="性別" /> <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" /> <asp:CommandField HeaderText="選擇" ShowSelectButton="True" /> <asp:CommandField HeaderText="編輯" ShowEditButton="True" /> <asp:CommandField HeaderText="刪除" ShowDeleteButton="True" /> </Columns> <RowStyle Horiz VerticalAlign="Middle" /> <EditRowStyle Horiz VerticalAlign="Middle" /> <HeaderStyle BackColor="#C0FFC0" /> <AlternatingRowStyle Horiz VerticalAlign="Middle" /> </asp:GridView> </div> </form> </body> </html> 4.后臺(tái)代碼 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { SqlConnection sqlCon; SqlCommand sqlCom; string strCon = "Data Source=(local);Database=GridView;Uid=sa;Pwd=sa"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sqlstr = "delete from Employee where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; sqlCon = new SqlConnection(strCon); sqlCom = new SqlCommand(sqlstr, sqlCon); sqlCon.Open(); sqlCom.ExecuteNonQuery(); sqlCon.Close(); bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { sqlCon = new SqlConnection(strCon); string sqlstr = "update Employee set 身份證號(hào)碼='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',姓名='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',員工性別='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',家庭住址='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].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; bind(); } public void bind() { string sqlStr = "select * from Employee"; sqlCon = new SqlConnection(strCon); SqlDataAdapter myda = new SqlDataAdapter(sqlStr, sqlCon); DataSet myds = new DataSet(); sqlCon.Open(); myda.Fill(myds, "Employee"); GridView1.DataSource = myds; GridView1.DataKeyNames = new string[] { "ID" }; GridView1.DataBind(); sqlCon.Close(); } } |
|
來(lái)自: 悟靜 > 《.net和asp.net》