使用GridView删除数据

第一步创建一个GridView

编辑列,填充字段属性

设置字段属性

其他字段根据数据库字段设置

接下来是添加“编辑”和“删除”了。

使用“添加新列”来添加列

添加“编辑”列

同理,添加“删除”

GridView设置效果如下:

初始化GridView

初始化数据从数据库查询的源码如下:

protected void bindSupplier(){SqlConnection con = new SqlConnection();con.ConnectionString = "Data Source=.;Initial Catalog=myPetShop;Integrated Security=True";SqlCommand cmd = new SqlCommand();cmd.Connection = con;cmd.CommandText = "select * from Supplier;";SqlDataAdapter da = new SqlDataAdapter();da.SelectCommand = cmd;DataSet ds = new DataSet();da.Fill(ds);GridView1.DataSource = ds.Tables[0];GridView1.DataBind();// 绑定并显示数据}

初始化:

if (!IsPostBack)
{// 绑定初始化供应商信息bindSupplier();
}

同时注意引入命名空间

运行程序浏览器效果图如下:

GridView的删除

其中“删除”链接需要处理的事件如下:

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){}protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){}

接下来就是写代码了:

        // GridView的RowDataBound事件在数据被分别绑定到行时触发// e.Row返回“删除”连接按钮的所在行对象protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){if (e.Row.RowType == DataControlRowType.DataRow)// 判断数据行{try{// 获取“删除”链接按钮LinkButton linkButton_delete = (LinkButton)e.Row.Cells[4].Controls[0];// 添加JavaScript代码实现客户端信息提示linkButton_delete.OnClientClick = "return confirm('您真要删除分类名称为" + e.Row.Cells[1].Text + "的记录吗?');";}catch{// 若try块有异常,不做任何处理}}}// 点击【删除】链接后触发的事件protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){SqlConnection con = new SqlConnection();con.ConnectionString = "Data Source=.;Initial Catalog=myPetShop;Integrated Security=True";SqlCommand cmd = new SqlCommand();cmd.Connection = con;//e.RowIndex:当前用户点击第几行的索引号//DataKey[0]:在主键列中查找第一个列cmd.CommandText = "delete from Supplier where SuppId="+ GridView1.DataKeys[e.RowIndex].Value.ToString()+";";con.Open();cmd.ExecuteNonQuery();con.Close();// 刷新GridView数据LinkButton_providerManage_Click(null, null);}

运行程序,报错:

错误原因是没有设置主键字段,解决:

再次运行就会删除成功了。

GridView的编辑

要处理的事件如下:

即:

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e){}protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){}

事件处理完整代码如下:

        // 进入编辑状态protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e){GridView1.EditIndex = e.NewEditIndex;// 绑定初始化数据bindSupplier();}// 取消编辑状态protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e){GridView1.EditIndex = -1;// 绑定初始化数据bindSupplier();}// 点击【更新】链接后触发的事件protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){SqlConnection con = new SqlConnection();con.ConnectionString = "Data Source=.;Initial Catalog=myPetShop;Integrated Security=True";SqlCommand cmd = new SqlCommand();cmd.Connection = con;cmd.CommandText = "update Supplier set Name='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text + "',Addr1='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text + "',Addr2='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text + "',City='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text + "',State='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text + "',Zip='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text + "',Phone='" + ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text + "' where SuppId=" + GridView1.DataKeys[e.RowIndex].Value.ToString() + ";";con.Open();cmd.ExecuteNonQuery();con.Close();// 刷新GridView数据LinkButton_providerManage_Click(null, null);}

编辑更新成功

利用GridView编辑和删除数据相关推荐

  1. ASP.NET 2.0数据教程之三十六 在DataList里编辑和删除数据

    导言 概述插入.更新和删除数据 里我们已经学习了如何使用GridView等控件来插入,更新删除数据.通过ObjectDataSource和其它数据控件仅仅只需要在智能标签里勾一下checkbox就完成 ...

  2. 如何在PowerPoint图表中编辑或删除数据

    Data trends change over time. As such, you may need to edit or remove data from a chart in a Microso ...

  3. GRIDVIEW控件删除数据前如何给予客户端提示

    在.net 2.0 中,GRIDVIEW控件有个GridView.RowDeleting 事件 ,我想在客户端删除数据前给予提示,弹出比如"确定删除吗?"的提示,并且根据用户的选择 ...

  4. GridView编辑EmptyDataTemplate控数据模板

    EmptyDataTemplate--空数据模板是GridView控件的属性之一.EmptyDataTemplate表示当记录为空时显示的定义内容.它用来获取或设置在GridView 控件绑定到不包含 ...

  5. 利用GridView显示主细表并一次编辑明细表所有数据的例子

    全部代码如下: ASPX: <%@ Page Language="C#" ValidateRequest="false" AutoEventWireup= ...

  6. GridView编辑删除操作

    第一种:使用DataSource数据源中自带的编辑删除方法,这样的不经常使用,在这里就不加说明了. 另外一种:使用GridView的三种事件:GridView1_RowEditing(编辑).Grid ...

  7. 利用yii2 gridview实现批量删除案例

    作者:白狼 出处:http://www.manks.top/article/yii2_gridview_deleteall本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...

  8. GridView 编辑,更新,删除 等操作

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1. ...

  9. 利用SQL语句对数据进行操作:插入、更新与删除数据行

    My目录 前言 一.T-SQL的组成 二.插入数据行 1.一次插入一行数据 2.一次插入多行数据 三.更新数据行 四.删除数据行 前言 结构化查询语言(Structured Query Languag ...

最新文章

  1. ascii码值为负数_作为2020年的程序员,你还在为编码问题困惑?
  2. oracle在哪些系统运行,ORACLE 查看系统运行情况
  3. python 使用UUID库生成唯一ID
  4. VMware linux虚拟机在线识别新添加磁盘
  5. 云上快报 | 阿里云混合云再攀新高
  6. ubuntu 安装libusb 编译自己写的程序 发现很多undefined(排除包含头文件的问题)
  7. eclipse加速之禁用 JS、jsp 等文件的语法验证
  8. 嵌入式Linux系统编程学习之二十一命名管道(FIFO)
  9. python教程视频-Python基础视频教程(600集)【传智播客精品教程】
  10. jquery easyui-----------tree
  11. 作为一个大学才开始入门学计算机编程的孩子想要的东西-----听我扯,你蛋疼,他菊紧,我开心...
  12. 解决注册表被禁用的问题
  13. iOS的崩溃率高于Android?来自听云的数据告诉你真相
  14. java接口里面可以定义变量么?
  15. STM32-IIC模拟从模式
  16. selenium处理富文本框
  17. 【springcloud】功能尝试(二)熔断器的三个功能
  18. php interface 抽象类,解析PHP中的抽象类(abstract class)和 接口(interface)
  19. Java实现视频边加载边播放(利用http请求头的Range)
  20. 解决video视频在ios中不能自动播放的问题

热门文章

  1. html embed页面无法播放视频播放,用embed标签在网页中嵌入avi格式的视频不能播放...
  2. 使用递归法求最大公约数
  3. springboot房屋租赁系统4yvi2[独有源码]了解毕业设计的关键考虑因素
  4. OpenCV 4.7 版本发布
  5. matlab绘制用颜色表示模值大小的箭头图
  6. 最全螺栓规格 l 拧紧到塑性区域的扭矩和预紧力大小
  7. grub和grub2区别
  8. php同步登陆方案,网站与phpwind论坛的同步登陆的解决方案
  9. 计算机网络体系之所以采用层次结构的主要原因是,计算机网络体系之所以采用层次结构的主要原因是()。...
  10. 网页文档幅制方法【网页文字如何免费复制】