当前位置:网站首页>Net基于girdview控件实现删除与编辑行数据
Net基于girdview控件实现删除与编辑行数据
2022-06-26 15:44:00 【51CTO】
代码如下:
using System;
using System.Collections.Generic;
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 修改数据 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 如果是为响应客户端回发而加载该页,则为 true;否则为 false。
if (!this.IsPostBack)
{
this.Bind();
}
}
/// <summary>
/// 绑定数据源数据到gridview控件
/// </summary>
private void Bind()
{
SqlConnection con = help.con();
con.Open();
string str = "select * from cs";
SqlCommand cmd = new SqlCommand(str,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "id"};
this.GridView1.DataBind();
da.Dispose();
cmd.Dispose();
con.Close();
}
/// <summary>
/// 行编辑事件给编辑行索引赋值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex= e.NewEditIndex;
this.Bind();
}
/// <summary>
/// 行更新前
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id= this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string cate= ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2]).Controls[0]).Text.ToString();
SqlConnection conn = help.con();
conn.Open();
string str = "update cs set Fcate='"+cate+"' where id="+id+"";
SqlCommand cmd = new SqlCommand(str, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
this.GridView1.EditIndex = -1;
this.Bind();
}
/// <summary>
/// 行取消时触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;//不指向任何的行
this.Bind();
}
/// <summary>
/// 行删除前执行的事
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id= this.GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlConnection conn = help.con();
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from cs where id="+id+"";
cmd.Connection = conn;
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
this.GridView1.EditIndex = -1;
this.Bind();
}
/// <summary>
/// 绑定数据控件的时候给删除按钮添加脚本事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//获取第一个单元格的第二个子项
if (((LinkButton)e.Row.Cells[0].Controls[2]).Text == "删除") {
((LinkButton)e.Row.Cells[0].Controls[2]).Attributes.Add("onclick", "return confirm('确定需要删除吗?')");
}
}
}
}
部分截图:


边栏推荐
- AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
- NFT transaction principle analysis (2)
- AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
- 2022 Beijing Shijingshan District specializes in the application process for special new small and medium-sized enterprises, with a subsidy of 100000-200000 yuan
- Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
- NFT 项目的开发、部署、上线的流程(2)
- IntelliJ idea -- Method for formatting SQL files
- [CEPH] Lock Notes of cephfs
- 音视频学习(一)——PTZ控制原理
- I want to know how to open an account through online stock? Is online account opening safe?
猜你喜欢
随机推荐
零知识 QAP 问题的转化
PCIe Capabilities List
「幹貨」NFT 上中下遊產業鏈全景分析
(一)keras手写数字体识别并识别自己写的数字
NFT Platform Security Guide (2)
【leetcode】112. Path sum - 113 Path sum II
TweenMax+SVG切换颜色动画场景
【leetcode】701. 二叉搜索树中的插入操作
Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
2 三种建模方式
简单科普Ethereum的Transaction Input Data
Golang temporary object pool optimization
Ansible自动化的运用
JS handwritten bind, apply, call
H5 close the current page, including wechat browser (with source code)
JVM笔记
feil_ The working directory on the left of uvission4 disappears
Solana扩容机制分析(1):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
NFT contract basic knowledge explanation
JS simple deepcopy (Introduction recursion)
![[wechat applet] event binding, do you understand?](/img/83/6242e972538d0423fd4155140bb521.png)







