打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
GridView中选中,编辑,取消,删除操作?

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.在页面中创建一个GridView,添加表中有的数据列
3.前台代码
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</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();
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
GridView 72般绝技
asp.net使用GridView删除更新ACCESS数据库里的数据问题!急!!!
GridView操作
GridView 使用方法总结(二)
VB.NET版的GridView经典使用(编辑,删除,分页,链接列)
gridview 编辑,删除,更新的用法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服