当前位置:网站首页>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('确定需要删除吗?')");
}
}
}
}
部分截图:


边栏推荐
- Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
- What is the difference between stm32f1 and gd32f1?
- 2 三种建模方式
- NFT 平台安全指南(2)
- Comprehensive analysis of discord security issues
- svg canvas画布拖拽
- [file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
- 效率超级加倍!pycharm十个小技巧就是这么神
- On which platform is it safe to buy shares and open an account? Ask for guidance
- NFT交易原理分析(1)
猜你喜欢

How to create your own NFT (polygon) on opensea

HW safety response

Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved

10 tf.data

NFT contract basic knowledge explanation

Transformation of zero knowledge QAP problem

OpenSea上如何创建自己的NFT(Polygon)

Use of abortcontroller

Everyone is a scientist free gas experience Mint love crash

AbortController的使用
随机推荐
Transformation of zero knowledge QAP problem
JS simple deepcopy (Introduction recursion)
Stepn novice introduction and advanced
音视频学习(三)——sip协议
STEPN 新手入门及进阶
Swiftui retrieves the missing list view animation
8 user defined evaluation function
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
Learning memory barrier
Svg canvas canvas drag
【微信小程序】事件绑定,你搞懂了吗?
7 自定义损失函数
Audio and video learning (I) -- PTZ control principle
音视频学习(一)——PTZ控制原理
NFT transaction principle analysis (2)
NFT Platform Security Guide (1)
01 backpack DP
Binding method of multiple sub control signal slots under QT
手写数字体识别,用保存的模型跑自己的图片
5000字解析:实战化场景下的容器安全攻防之道