当前位置:网站首页>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('确定需要删除吗?')");
}
}
}
}
部分截图:
边栏推荐
- /etc/profile、/etc/bashrc、~/. Bashrc differences
- Svg animation around the earth JS special effects
- Evaluate:huggingface detailed introduction to the evaluation index module
- svg上升的彩色气泡动画
- golang 1.18 go work 使用
- When a project with cmake is cross compiled to a link, an error cannot be found So dynamic library file
- Interview pit summary I
- [thinking] what were you buying when you bought NFT?
- AbortController的使用
- 7 自定义损失函数
猜你喜欢
High frequency interview 𞓜 Flink Shuangliu join
Comprehensive analysis of discord security issues
Solana扩容机制分析(1):牺牲可用性换取高效率的极端尝试 | CatcherVC Research
人人都当科学家之免Gas体验mint爱死机
[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
NFT合约基础知识讲解
简单科普Ethereum的Transaction Input Data
NFT Platform Security Guide (1)
TweenMax+SVG切换颜色动画场景
9 Tensorboard的使用
随机推荐
Tweenmax+svg switch color animation scene
What is the difference between stm32f1 and gd32f1?
NFT交易原理分析(1)
8 user defined evaluation function
Solana capacity expansion mechanism analysis (1): an extreme attempt to sacrifice availability for efficiency | catchervc research
AbortController的使用
Don't remove custom line breaks on reformat
01 backpack DP
[CEPH] Lock Notes of cephfs
[CEPH] Introduction to cephfs caps
在哪个平台买股票开户安全?求指导
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
JVM笔记
CNN优化trick
Summary of data interface API used in word search and translation applications
【leetcode】48. Rotate image
Selenium saves elements as pictures
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
5000字解析:实战化场景下的容器安全攻防之道
基于 MATLAB的自然过渡配音处理方案探究