【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). ...
随机推荐
- 【Linux命令】modprobe命令
modprobe(module probe)命令 用于自动处理可载入模块. 1)语法 modprobe [-acdlrtvV][--help][模块文件][符号名称 = 符号值] 2)补充 modpr ...
- x86—EFLAGS寄存器详解(转载)
鉴于EFLAGS寄存器的重要性,所以将这一部分内容从处理器体系结构及寻址模式一文中单独抽出另成一文,这部分内容主要来自Intel Developer Mannual,在后续的内核系列中遇到的许多和EF ...
- tensorflow学习笔记——自编码器及多层感知器
1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...
- CF EDU - E. Lomsat gelral 树上启发式合并
学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...
- SQL数据同步到ELK(二)- Elastic Search 安装
开篇废话 没错,前面扯了一堆SQL SERVER,其实我连Elastic Search根本没动手玩过(是不是与时代有点脱节了?),那今天我就准备尝试安装一个ELK的简单集群出来(这个集群是使用我的小米 ...
- Request请求的应用
1.通过request获得请求行 获得客户端的请求方式:String getMethod() 获得请求的资源: String getRequestURI() StringBuffer getReq ...
- Linux root 用户下 selenium 运行chrome --no-sandbox的问题的解决
#coding = utf-8 from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_opt ...
- spring boot启动加载项CommandLineRunner
spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...
- Java Web总结(一)-- 入门
一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源( ...
- BZOJ 刷题总结(持续更新)
本篇博客按照题号排序(带*为推荐题目) 1008 [HNOI2008]越狱 很经典的题了..龟速乘,龟速幂裸题,, 1010 [HNOI2008]玩具装箱toy* 斜率优化 基本算是裸题. 1012 ...