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

前台代码:

<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. 如何使用有道云笔记的Markdown----初级版?

    我一般整理笔记用的是用有道云笔记,在这里,Markdown怎么用? 什么是Markdown?Markdown是一种轻量级的「标记语言」,通常为程序员群体所用,目前它已是全球最大的技术分享网站 GitH ...

  2. 前端利器躬行记(3)——webpack基础

    webpack是一个静态模块打包器,此处的模块可以是任意文件,包括Sass.TypeScript.模板和图像等.webpack可根据输入文件的依赖关系,打包输出浏览器可识别的JavaScript.CS ...

  3. Spring框架介绍及使用(转载)

    原文链接 Spring框架—控制反转(IOC) 1 Spring框架概述1.1 什么是SpringSpring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod ...

  4. FZU - 2150-Fire Game BFS-枚举

    Fire Game 题意: 两个小朋友可以任选一块草地点火,草地可以不同,也可以相同,问最少的烧光草地的时间. 思路: 一开始看到这个以为是联通块计数,没想到这道题通过枚举两个起始点作为队列的初始点, ...

  5. cogs 2652. 秘术「天文密葬法」(0/1分数规划 长链剖分 二分答案 dp

    http://cogs.pro:8080/cogs/problem/problem.php?pid=vSXNiVegV 题意:给个树,第i个点有两个权值ai和bi,现在求一条长度为m的路径,使得Σai ...

  6. HDU 4322Candy 最大费用最大流

    由于被小孩子不喜欢的糖果的对小孩产生的效力是一样的,所以我们在网络流的时候先不考虑. 1 - 源点0到1~N个糖果,容量为1,费用为02 - 根据like数组,like[i][j] == 1时在糖果j ...

  7. 牛客暑假多校第五场 I vcd

    这个题目一个队友没读懂, 然后我读错了题目, 还让他堆了半天的公式写了半天的代码, 交上去一直0.0, 另一队友问题目有没有读错, 我坚持没有读错, 然后坑了2个小时的时间,不然应该会早一点做出来. ...

  8. CF 435B Little Pony and Harmony Chest

    Little Pony and Harmony Chest 题解: 因为 1 <= ai <= 30 所以  1 <= bi <= 58, 因为 59 和 1 等效, 所以不需 ...

  9. HDU dp递推 母牛的故事 *

    母牛的故事 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  10. 为什么Hashtable ConcurrentHashmap不支持key或者value为null

    ConcurrentHashmap HashMap和Hashtable都是key-value存储结构,但他们有一个不同点是 ConcurrentHashmap.Hashtable不支持key或者val ...