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

前台代码:

<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. MSIL实用指南-this的生成

    C#关键字是非静态方法体内部,用Ldarg_0指代this例子ilGenerator.Emit(OpCodes.Ldarg_0);

  2. Win10下安装python3.x+pycharm+autopep8

    一.安装Python3.X 1.Pythong官方网站:http://python.org/getit/   下载windows的安装包.有以下几个选项: 这里选择windows x86-64 exc ...

  3. Interger等包装类的比较

    Integer a = 1; integer b = 1; integer c = 500; integer d=500; System.out.print(a==b); System.out.pri ...

  4. 用java实现取1-100之间的99个不重复的随机数 然后输出没有被取出的数字

    package cn.kgc.springtest2.demo1.dao; import java.util.BitSet; /** * @author * @create 2019-08-02 17 ...

  5. 从Linux服务器下载上传文件

    首先要确定好哪两种的连接:Linux常用的有centors和unbantu两种版本,PC端Mac和Windows 如果在两个Linux之间传输,或Linux和Mac之间传输可以使用scp命令,类似于s ...

  6. C# 活体检测

    活体检测有多种情形,本文所指:从摄像头获取的影像中判断是活体,还是使用了相片等静态图片. 场景描述 用户个人信息中上传了近照,当用户经过摄像头时进行身份识别. 此时,如果单纯的使用摄像头获取的影像进行 ...

  7. React Hooks 你不来了解下?

    前言 最近在看 React 的新语法-- React Hooks,只能一句话概括:React 语法真的是越来越强大,越写代码越少. 强烈推荐还没看 React Hooks 的同学去学习下,这会让你写r ...

  8. application.properties 乱码 (已验证)

    1.打开Eclipse或MyEclipse 2.选择window-Preferences-content Types-Text-Java Properties File 3.将Java Propert ...

  9. spring boot的多环境部署

    需求:不同的环境有不同的开关属性,比如开发系统,需要关闭短信,微信的通知功能.而演示环境,线上环境则需要打开这些配置. 那么,如何做到呢?--->在properties.application配 ...

  10. 第1次作业:使用Packet Tracer分析HTTP数据包

    个人信息:      •  姓名:李微微       •  班级:计算1811       •  学号:201821121001 一.摘要 本文将会描述使用Packet Tracer工具用到的网络结构 ...