zoukankan      html  css  js  c++  java
  • 利用Gridview 控件提供的默认编辑,删除操作

    利用Gridview控件自身所带的默认编辑,删除操作 数据:

    后台代码的书写:

    View Code
    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3 if (!IsPostBack)
    4 {
    5 databind();
    6 }
    7
    8 }
    9 public void databind()
    10 {
    11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
    12 SqlCommand cmd = new SqlCommand("Select * from Customers", con);
    13 SqlDataAdapter da = new SqlDataAdapter(cmd);
    14 DataSet ds = new DataSet();
    15 da.Fill(ds);
    16 this.GridView1.DataSource = ds.Tables[0];
    17 this.GridView1.DataKeyNames = new string[] { "CustomerID"};
    18 this.GridView1.DataBind();
    19 }
    20 /// <summary>
    21 /// 编辑
    22 /// </summary>
    23 /// <param name="sender"></param>
    24 /// <param name="e"></param>
    25   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    26 {
    27 this.GridView1.EditIndex = e.NewEditIndex;
    28 databind();
    29 }
    30 /// <summary>
    31 /// 取消
    32 /// </summary>
    33 /// <param name="sender"></param>
    34 /// <param name="e"></param>
    35 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    36 {
    37 this.GridView1.EditIndex = -1;
    38 databind();
    39 }
    40 /// <summary>
    41 /// 数据更新
    42 /// </summary>
    43 /// <param name="sender"></param>
    44 /// <param name="e"></param>
    45 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    46 {
    47 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
    48 SqlCommand cmd = new SqlCommand();
    49 cmd.Connection = con;
    50 cmd.CommandText = "Update Customers set CompanyName =@CompanyName ,ContactName=@ContactName where CustomerID=@CustomerID";
    51 SqlParameter[] sp = new SqlParameter[3];
    52 sp[0] = new SqlParameter("@CompanyName",SqlDbType.NVarChar,40);
    53 sp[1] = new SqlParameter("@ContactName",SqlDbType.NVarChar,30);
    54 sp[2] = new SqlParameter("@CustomerID",SqlDbType.NChar ,5);
    55 sp[0].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
    56 sp[1].Value = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
    57 sp[2].Value = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
    58 cmd.Parameters.AddRange(sp);
    59 if (con.State == ConnectionState.Closed)
    60 {
    61 con.Open();
    62 }
    63 cmd.ExecuteNonQuery();
    64 con.Close();
    65 this.GridView1.EditIndex = -1;
    66 databind();
    67 }
    68 /// <summary>
    69 /// 数据删除
    70 /// </summary>
    71 /// <param name="sender"></param>
    72 /// <param name="e"></param>
    73 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    74 {
    75 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
    76 SqlCommand cmd = new SqlCommand();
    77 cmd.Connection = con;
    78 cmd.CommandText = "Delete Customers where CustomerID=@CustomerID";
    79 SqlParameter sp = new SqlParameter("@CustomerID", SqlDbType.NChar, 5);
    80 sp.Value = this.GridView1.DataKeys[e.RowIndex].Value;
    81 cmd.Parameters.Add(sp);
    82 if (con.State == ConnectionState.Closed)
    83 {
    84 con.Open();
    85 }
    86 cmd.ExecuteNonQuery();
    87 con.Close();
    88 databind();
    89 }
    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    CentOS-7 虚拟机意外断电后的数据恢复
    CentOS7 搭建VNC 服务
    CentOS7-ulimit
    CentOS-7 初始化配置
    Centos Bond设置实例
    服务检测脚本
    sshd服务
    input常用属性
    前端工具-Sublime、WebStorm-快捷方式使用
    随机抽选效果、随机抽选红色球
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1962571.html
Copyright © 2011-2022 走看看