资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

GridView如何使用自带编辑删除更新功能-创新互联

这篇文章主要介绍GridView如何使用自带编辑删除更新功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

在成都网站设计、成都网站建设、外贸网站建设中从网站色彩、结构布局、栏目设置、关键词群组等细微处着手,突出企业的产品/服务/品牌,帮助企业锁定精准用户,提高在线咨询和转化,使成都网站营销成为有效果、有回报的无锡营销推广。创新互联公司专业成都网站建设10年了,客户满意度97.8%,欢迎成都创新互联客户联系。

效果图:

GridView如何使用自带编辑删除更新功能

GridView如何使用自带编辑删除更新功能

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridView_bianjidelete.aspx.cs" Inherits="gridView_bianjidelete" %>




 


 
 
                                                                                                          
 

后台代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class gridView_bianjidelete : System.Web.UI.Page
{//清清月儿http://blog.csdn.net/21aspnet
 SqlConnection sqlcon;
 SqlCommand sqlcom;
 string strCon = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
   bind();
  }
 }
 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 {
  GridView1.EditIndex = e.NewEditIndex;
  bind();
 }
 //删除之后重新绑定
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
  string sqlstr = "delete from product where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
  sqlcon = new SqlConnection(strCon);
  sqlcom = new SqlCommand(sqlstr, sqlcon);
  sqlcon.Open();
  sqlcom.ExecuteNonQuery();
  sqlcon.Close();
  GridView1.DataBind();
  bind();
 }
 //更新
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
  sqlcon = new SqlConnection(strCon);
  string sqlstr = "update product set name='"
   + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',stock='"
   + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].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;
  // GridView1.DataBind();
  bind();
 }
 //取消
 protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
 {
  GridView1.EditIndex = -1;
  bind();
 }
 //绑定
 public void bind()
 {
  string sqlstr = "select * from product p,Uuser u where p.userid=u.id";
  sqlcon = new SqlConnection(strCon);
  SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
  DataSet myds = new DataSet();
  sqlcon.Open();
  myda.Fill(myds, "datatable");
  GridView1.DataSource = myds;
  GridView1.DataKeyNames = new string[] { "id" };//主键
  GridView1.DataBind();
  sqlcon.Close();
 }
}

以上是“GridView如何使用自带编辑删除更新功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


当前题目:GridView如何使用自带编辑删除更新功能-创新互联
转载源于:http://cdkjz.cn/article/isheh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220