GridView选中,编辑,取消,删除

员工管理系统:数据展示与操作
本文介绍了一个基于ASP.NET的员工管理系统,该系统通过ADO.NET连接到SQL Server数据库,实现员工信息的显示、编辑、删除和更新。系统利用GridView组件进行数据展示,并通过绑定数据集来动态更新界面。此外,提供了数据删除、编辑和取消编辑的功能。

效果图:

后台代码:
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 conn=new SqlConnection();
    SqlCommand cmd=new SqlCommand();
    string strCon = "Data Source=.\\SQLExpress;Initial Catalog=Message;User ID=sa;Password=123";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)        //if(不是回发),点击一个按钮,页面不会重新生成
        {
            bind();
        }
    }
   
    public void bind()
    {
        string sqlStr = "select * from Employee";
        conn = new SqlConnection(strCon);                         //连接字符串
        SqlDataAdapter myda = new SqlDataAdapter(sqlStr, conn);   //创建DataAdapter数据适配器实例
        DataSet myds = new DataSet();                               //创建DataSet实例
        conn.Open();                                            //打开数据库
        myda.Fill(myds, "Employee");                             //使用DataAdapter的Fill方法填充
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "ID" };             
        GridView1.DataBind();                                    //绑定数据
        conn.Close();                                          //关闭数据库
    }
    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from Employee where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        conn = new SqlConnection(strCon);
        cmd = new SqlCommand(sqlstr, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
    }
    protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;    //设置gridview的编辑项。gridview.编辑项下标=e.新的编辑项下标。
        bind();
    }
    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
    {
        conn = 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() + "'";
        cmd = new SqlCommand(sqlstr, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        GridView1.EditIndex = -1;
        bind();
    }
}


前台主要代码:
                            ... ...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
                            <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 ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值