GridView实例

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class pages_GridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindToGridView();
    }
    string conStr = "server=.;database=AttendanceDataBase;uid=sa;pwd=yujie1127";
    public void BindToGridView()
    {
        SqlConnection con = new SqlConnection(conStr);
        con.Open();
        string sql = "select * from Employees";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        con.Close();
        this.GridView1.DataSource = ds.Tables[0];
        this.DataBind();
    }
    //分页实现
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindToGridView();
    }
    //取消编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindToGridView();
    }
    //删除行
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string EmployeeId =this.GridView1.DataKeys[e.RowIndex][0].ToString();
        DeleteEmployee( EmployeeId);
        this.GridView1.EditIndex = -1;
        BindToGridView();
    }
    //编辑行
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        BindToGridView();
    }
    //更新行
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string EmployeeId = this.GridView1.DataKeys[e.RowIndex][0].ToString();
        string EmployeeName =
            ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
        string Address =
            ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
        string Phone=
            ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
        UpdateEmployee(EmployeeId, EmployeeName, Address, Phone);
        this.GridView1.EditIndex = -1;
        BindToGridView();
    }
    //更新员工表
    public void UpdateEmployee(string EmployeeId, string EmployeeName,
        string Address, string Phone)
    {
        SqlConnection con = new SqlConnection(conStr);
        con.Open();
       // string sql = "update Employees set EmployeeName='" + EmployeeName + "',Sex='" + Sex + "', BirthDate='" + BirthDate + "',Address='" + Address + "',Phone='" + Phone + "' where Phone='" + Phone + "'";
        string sql = "update Employees set EmployeeName=@EmployeeName,Address=@Address,Phone=@Phone where EmployeeId=@EmployeeId";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.AddWithValue("@EmployeeName", EmployeeName);;
        cmd.Parameters.AddWithValue("@Address", Address);
        cmd.Parameters.AddWithValue("@Phone", Phone);
        cmd.Parameters.AddWithValue("@EmployeeId", EmployeeId);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
    }
    //根据员工编号删除员工
    public void DeleteEmployee(string EmployeeId)
    {
        SqlConnection con = new SqlConnection(conStr);
        con.Open();
        string sql = "delete from Employees where EmployeeId=@EmployeeId";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.AddWithValue("@EmployeeId", EmployeeId);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值