当前位置:网站首页>Net based on girdview control to delete and edit row data
Net based on girdview control to delete and edit row data
2022-06-26 16:04:00 【51CTO】
The code is as follows :
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 Modifying data : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// If the page is loaded in response to a client postback , Then for true; Otherwise false.
if (!this.IsPostBack)
{
this.Bind();
}
}
/// <summary>
/// Bind data source data to gridview Control
/// </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>
/// The row edit event assigns a value to the edit row index
/// </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>
/// Before line update
/// </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>
/// Triggered when the line is canceled
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;// Does not point to any line
this.Bind();
}
/// <summary>
/// What to do before deleting a row
/// </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>
/// Add script events to the delete button when binding data controls
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Get the second child of the first cell
if (((LinkButton)e.Row.Cells[0].Controls[2]).Text == " Delete ") {
((LinkButton)e.Row.Cells[0].Controls[2]).Attributes.Add("onclick", "return confirm(' Are you sure you want to delete ?')");
}
}
}
}
Partial screenshots :


边栏推荐
- C language reading data
- 为什么图像分割任务中经常用到编码器和解码器结构?
- Interview pit summary I
- STEPN 新手入门及进阶
- svg野人动画代码
- Summary of data interface API used in word search and translation applications
- Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
- Practice of federal learning in Tencent micro vision advertising
- 神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读
- IAR工程适配GD32芯片
猜你喜欢

零知识 QAP 问题的转化

Svg rising Color Bubble animation

Tencent Peking University's sparse large model training acceleration program het was selected into the VLDB of the international summit

大话领域驱动设计——表示层及其他

Everyone is a scientist free gas experience Mint love crash

Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统

1 张量的简单使用

Big talk Domain Driven Design -- presentation layer and others

(一)keras手写数字体识别并识别自己写的数字

Have you ever had a Kindle with a keyboard?
随机推荐
Common properties of XOR and addition
手机上怎么开户?在线开户安全么?
Canvas three dot flashing animation
(DFS search) acwing 2005 horseshoe
HW safety response
svg上升的彩色气泡动画
What is the process of switching C # read / write files from user mode to kernel mode?
6 custom layer
TweenMax+SVG切换颜色动画场景
Stepn débutant et avancé
7 自定义损失函数
Simple use of tensor
Tencent Peking University's sparse large model training acceleration program het was selected into the VLDB of the international summit
5000 word analysis: the way of container security attack and defense in actual combat scenarios
Comprehensive analysis of discord security issues
Application of ansible automation
Oilfield exploration problems
Svg canvas canvas drag
Cookie和Session详解
How to configure and use the new single line lidar