代码都是基础操作,后续功能还会更新,如有问题欢迎提出和提问.......

前台代码:

<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的编辑、更新、取消、删除以及相关基础操作代码的更多相关文章

  1. gridview的编辑,更新,取消,自动分页等

    gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉 添加boundfield(绑定列)将其datafield设置为productname,headertext设置为 ...

  2. AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除

    AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...

  3. JAVA之Mybatis基础入门二 -- 新增、更新、删除

    上一节说了Mybatis的框架搭建和简单查询,这次我们来说一说用Mybatis进行基本的增删改操作: 一. 插入一条数据 1.首先编写USER.XML(表的xml)使用insert元素,元素写在map ...

  4. GridView选中,编辑,取消,删除代码

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 2.GridView选中,编辑,取消,删除: 效果图: 后台代码:你可以使用sqlhelper,本文没用。代码如下 ...

  5. python对MySQL进行数据的插入、更新和删除之后需要commit,数据库才会真的有数据操作。(待日后更新)

    今天在尝试用下面的python代码对MySQL进行数据的插入.更新和删除时, 突然发现代码执行成功, 通过代码查询也显示数据已经插入或更新, 但是当我在MySQL客户端通过SQL语句查询时, 数据库中 ...

  6. GridView总结二:GridView自带编辑删除更新

    GridView自带编辑删除更新逻辑很简单:操作完,重新绑定.总结总结,防止忘记... 效果图: 前台代码: <%@ Page Language="C#" AutoEvent ...

  7. GridView中的编辑和删除按钮,执行更新和删除代码之前的更新提示或删除提示

    在GridView中,可以通过设计界面GridViewr任务->编辑列->CommandField,很简单的添加的编辑和删除按钮 在前台源码中,可以看到GridView自动生成了两个列. ...

  8. GridView编辑、取消按钮自定义控件

    这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. < ...

  9. GridView控件中插入自定义删除按钮并弹出确认框

    GridView控件中插入自定义删除按钮,要实现这个功能其实有多种方法,这里先记下我使用的方法,以后再添加其他方法. 一.实现步骤 1.在GridView中添加模板列(TemplateField). ...

随机推荐

  1. dataGrip连接数据库失败[08001]的一种可能原因

      我使用的是mac系统,并且由于机型较老,容量不高,为减轻系统负荷,没有设置开机自动启动MYSQL服务.这导致我在一次重启后,重新开启服务,然后就出现了dataGrip连接不上数据库: 在网上查找相 ...

  2. spss数据分析可以被人工智能替换吗

    作为一名需要对课题进行研究的大学生,我在日常学习中经常需要用到spss,虽然老师上课已经初步教了我如何用这个软件,然而,在使用过程中我还是遇到了许多问题.具体来说,就是这个软件在很多地方都不够与时俱进 ...

  3. 微擎 人人商城 对接京东vop 对接京东商品,同步商品 地址,库存,价格,上下架等。(二) 设置后台管理界面

    昨天提到了,由于vop商品池未开通,故对接工作只能暂缓,现在要做一个专门针对vop商品的后台管理, 老规矩,先上设计链路图 因为后台本来就是有比较完善的商品管理系统, 所以我们只是针对vop 进行简单 ...

  4. Maximum Product UVA - 11059

    Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the ...

  5. adb 端口冲突

    第一种方法:可以设置另一个端口 第二种方法: http://blog.csdn.net/hwl1314/article/details/52538023 Android studio 5037端口被占 ...

  6. CenTOS7使用ACL控制目录权限,只给某个用户访问特定目录

    前言 Linux 基本的权限控制仅可以对所属用户.所属组.其他用户进行的权限控制,而不能精确地控制每个用户的权限.ACL 规则就是用来解决这个问题的. 使用 ACL 规则,我们可以针对单一账户设置文件 ...

  7. python简介及详细安装方法

    1.Python简介 1.1 Python是什么 相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是 ...

  8. JavaScript数组和伪数组

    伪数组和数组 记住一句话: 伪数组是一个Object,数组是Array. 对象和数组之间的关系 JavaScript的内置函数继承与 Object.prototype. 可以认为new Array() ...

  9. Vert.x学习之 Web Client

    Vert.x Web Client 原文档 组件源码 组件示例 中英对照表 Pump:泵(平滑流式数据读入内存的机制,防止一次性将大量数据读入内存导致内存溢出) Response Codec:响应编解 ...

  10. sql 行列互转

    1.行转列 现有数据: 期望数据: 1.1建表建数据 IF OBJECT_ID('temp_20170701','u') IS NOT NULL DROP TABLE temp_20170701 CR ...