小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

GridView中選中,編輯,取消,刪除操作?

 悟靜 2012-02-25

GridView中選中,編輯,取消,刪除操作?

1.CREATE TABLE [dbo].[Employee] (
[ID] [int] NOT NULL ,
[身份證號碼] [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.在頁面中創(chuàng)建一個GridView,添加表中有的數(shù)據(jù)列
3.前臺代碼
<%@ 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>無標(biāo)題頁</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="身份證號碼" HeaderText="用戶ID" />
                <asp:BoundField DataField="姓名" HeaderText="用戶姓名" />
                <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.后臺代碼
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 身份證號碼='" + ((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();
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多