日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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

 悟靜 2012-02-25

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();
    }
}

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多