【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码
代码都是基础操作,后续功能还会更新,如有问题欢迎提出和提问.......
前台代码:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" AutoGenerateColumns="False" CellPadding="" CellSpacing="" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="Page_Load" DataKeyNames="SID" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="" OnRowDataBound="GridView1_RowDataBound" >
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
<Columns>
<asp:BoundField DataField="password" HeaderText="编号" ReadOnly="true" />
<asp:BoundField DataField="SID" HeaderText="学号" ReadOnly="true" />
<asp:BoundField DataField="name" HeaderText="姓名" ControlStyle-Width=""/>
<asp:BoundField DataField="sex" HeaderText="性别" ControlStyle-Width=""/>
<asp:BoundField DataField="age" HeaderText="年龄" ControlStyle-Width=""/>
<asp:BoundField DataField="address" HeaderText="地址" ControlStyle-Width=""/>
<asp:BoundField DataField="phone" HeaderText="电话" ControlStyle-Width=""/>
<asp:BoundField DataField="qq" HeaderText="QQ" ControlStyle-Width=""/>
<asp:BoundField DataField="time" HeaderText="注册时间" HtmlEncode="false" DataFormatString="{0:yyyy-M-dd}" ReadOnly="true"/>
<asp:CommandField ButtonType="Button" HeaderText="操作" ShowDeleteButton="True" ShowEditButton="True"/>
</Columns>
</asp:GridView>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; namespace ado.netDemo1
{
public partial class Test1 : System.Web.UI.Page
{
SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
//string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
//SqlConnection conn = new SqlConnection(connStr);
//conn.Open();
//string sql = "select * from tb_Students";
//SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
//DataSet dataset = new DataSet();
//adapter.Fill(dataset);
//conn.Close();
//GridView1.DataSource = dataset.Tables[0].ToString();
//GridView1.DataBind(); if(!IsPostBack)
{
shuaxin();
} } //刷新数据
private void shuaxin() { connStr.Open();
string sql = "select * from tb_Students";
SqlDataAdapter da = new SqlDataAdapter(sql, connStr);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[].Rows.Count > ) { GridView1.DataSource = ds; GridView1.DataBind(); }
GridView1.DataKeyNames = new string[] { "SID" };
connStr.Close(); } //编辑
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
shuaxin();
}
//取消编辑
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -;
shuaxin();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//( Name, Password, Sex, Age, Address, Phone, QQ)
string sql = "update tb_Students set Name='"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString().Trim() +"',Sex='"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString().Trim() + "',Age='"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString().Trim() + "',Address='"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString().Trim() + "',Phone='"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString().Trim() + "',QQ='"
+((TextBox)(GridView1.Rows[e.RowIndex].Cells[].Controls[])).Text.ToString().Trim() +"'where SID='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString().Trim()+ "'";
connStr.Open();
SqlCommand cmd = new SqlCommand(sql,connStr);
int EXQ = cmd.ExecuteNonQuery();
if (EXQ == )
{
Response.Write("<script type=text/javascript>alert('更新成功!')</script>");
}
else
{
Response.Write("<script type=text/javascript>alert('更新失败!')</script>");
}
connStr.Close();
GridView1.EditIndex = -;
shuaxin(); } //删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{ string sql = "delete from tb_Students where SID='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
connStr.Open();
SqlCommand cmd = new SqlCommand(sql,connStr);
int EXQ = cmd.ExecuteNonQuery();
if (EXQ == )
{
Response.Write("<script type=text/javascript>alert('删除成功!')</script>");
}
else
{
Response.Write("<script type=text/javascript>alert('删除失败!')</script>");
}
connStr.Close();
shuaxin(); }
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
shuaxin();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
//编号
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ //编号以及鼠标经过的背景更改
if (e.Row.RowType == DataControlRowType.DataRow)
{
////鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#95CACA'");
////鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F0F0F0'"); ////当有编辑列时,避免出错,要加的RowState判断
//if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
//{
// ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
//} }
if (e.Row.RowIndex != -)
{
int id = e.Row.RowIndex + ;
e.Row.Cells[].Text = id.ToString();
} } }
}
运行截图:

【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码的更多相关文章
- gridview的编辑,更新,取消,自动分页等
gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉 添加boundfield(绑定列)将其datafield设置为productname,headertext设置为 ...
- AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除
AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...
- JAVA之Mybatis基础入门二 -- 新增、更新、删除
上一节说了Mybatis的框架搭建和简单查询,这次我们来说一说用Mybatis进行基本的增删改操作: 一. 插入一条数据 1.首先编写USER.XML(表的xml)使用insert元素,元素写在map ...
- GridView选中,编辑,取消,删除代码
原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 2.GridView选中,编辑,取消,删除: 效果图: 后台代码:你可以使用sqlhelper,本文没用。代码如下 ...
- python对MySQL进行数据的插入、更新和删除之后需要commit,数据库才会真的有数据操作。(待日后更新)
今天在尝试用下面的python代码对MySQL进行数据的插入.更新和删除时, 突然发现代码执行成功, 通过代码查询也显示数据已经插入或更新, 但是当我在MySQL客户端通过SQL语句查询时, 数据库中 ...
- GridView总结二:GridView自带编辑删除更新
GridView自带编辑删除更新逻辑很简单:操作完,重新绑定.总结总结,防止忘记... 效果图: 前台代码: <%@ Page Language="C#" AutoEvent ...
- GridView中的编辑和删除按钮,执行更新和删除代码之前的更新提示或删除提示
在GridView中,可以通过设计界面GridViewr任务->编辑列->CommandField,很简单的添加的编辑和删除按钮 在前台源码中,可以看到GridView自动生成了两个列. ...
- GridView编辑、取消按钮自定义控件
这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. < ...
- GridView控件中插入自定义删除按钮并弹出确认框
GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...
随机推荐
- SPSS数据分析方法不知道如何选择
一提到数学,高等数学,线性代数,概率论与数理统计,数值分析,空间解析几何这些数学课程,头疼呀.作为文科生,遇见这些课程时,通常都是各种寻求帮助,班上有位宅男数学很厉害,各种被女生‘围观’,这数学为 ...
- 《快照读、当前读和MVCC》
1.快照读 快照读是基于 MVCC 和 undo log 来实现的,适用于简单 select 语句,避免了幻读. 读已提交:一个事务内操作一条数据,可以查询到另一个已提交事务操作同一条数据的最新值.( ...
- CF EDU 1101D GCD Counting 树形DP + 质因子分解
CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...
- 牛客小白月赛8 - E - 诡异数字 数位DP
牛客小白月赛8 - E - 诡异数字 题意: 求区间中,满足限制条件的数字的个数. 限制条件就是某些数字不能连续出现几次. 思路: 比较裸的数位DP, DP数组开一个dp[len][x][cnt] 表 ...
- UVA10330拆点最大流
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #i ...
- 在javascript中的浏览器兼容问题以及兼容浏览器汇总(默认事件,阻止冒泡,事件监听。。。)以及解决方式详解
在javascript中常见的浏览器兼容问题,以及解决方式. 在前端工作当中我们遵循这样的原则:渐进增强和优雅降级 渐进增强(progressive enhancement): 针对低版本浏览器进 ...
- Java微服务(二):服务消费者与提供者搭建
本文接着上一篇写的<Java微服务(一):dubbo-admin控制台的使用>,上篇文章介绍了docker,zookeeper环境的安装,并参考dubbo官网演示了dubbo-admin控 ...
- Mysql的事务及行级锁
转自:http://www.cnblogs.com/edwinchen/p/4171866.html 以签到为例,每个用户每天只能签到一次,那么怎么去判断某个用户当天是否签到呢?因为当初表设计的时候, ...
- 【Spring】AOP注解方式实现机制
一.概述 二.@EnableAspectJAutoProxy 注解分析 三.分析AnnotationAwareAspectJAutoProxyCreator 四.执行流程 1. registerBea ...
- JavaScript里处理数字的一些常用方法
1.toString() 把字符串转换为数值. let num = 123; console.log(typeof(num)); //number console.log(typeof(num.toS ...