这部份小内容很想写下来了,因为是基础中的基础,但是近来用的比较少,又温习了一篇,发现有点陌生了,所以,还是写一下吧。

方法一:使用Gridview本身自带的事件处理,代码如下(注意:每次操作完都得重新绑定,且删除列要转为模板列):

 protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ShowCategories();
ShowProducts();
}
} private void ShowProducts()
{
DataTable dt = NorthWind.DBHelp.GetTable("SELECT Product.产品ID, Product.产品名称, Supply.公司名称, Supply.城市, Category.类别名称, Category.图片, Product.单位数量, Product.单价, Product.库存量, Product.中止 FROM Category INNER JOIN Product ON Category.类别ID = Product.类别ID INNER JOIN Supply ON Product.供应商ID = Supply.供应商ID where Product.类别ID="+int.Parse(this.DropDownList1.SelectedValue));
this.GridView1.DataSource = dt;
this.GridView1.DataKeyNames = new string[]{"产品ID"};//设置数据操作的主键列
this.GridView1.DataBind();
} private void ShowCategories()
{
DataTable dt = NorthWind.DBHelp.GetTable("select * from Category");
this.DropDownList1.DataSource = dt;
this.DropDownList1.DataTextField = "类别名称";
this.DropDownList1.DataValueField = "类别ID";
this.DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowProducts();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{ }
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//将当前要编辑的行的索引告诉GridView
this.GridView1.EditIndex = e.NewEditIndex;
ShowProducts();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//更新数据库
decimal price = decimal.Parse(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")).Text);
int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
//更新到数据库
NorthWind.DBHelp.ExecuteNoQuery("update Product set 单价="+price+" where 产品ID="+productid);
//重新绑定
this.GridView1.EditIndex = -1;
ShowProducts();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
ShowProducts();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//删除数据
int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
//更新到数据库
NorthWind.DBHelp.ExecuteNoQuery("delete from Product where 产品ID=" + productid);
//重新绑定
this.GridView1.EditIndex = -1;
ShowProducts();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//当我们对数据源中的每一行进行数据绑定时触发的事件
if (e.Row.RowType == DataControlRowType.DataRow)//判断绑定时候该行的状态时数据行还是其他类型的模板
{
//添加客户端确认
LinkButton lnkDelete = (LinkButton)e.Row.Cells[10].FindControl("lnkDelete");
lnkDelete.Attributes.Add("onclick", "return confirm('你确认删除吗?')");
//光棒效果
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ff66cc';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
//获取全选的复选框
CheckBox chkSelectAll = (CheckBox)sender;
//获取每一行的复选框
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
CheckBox chkSelect = (CheckBox)this.GridView1.Rows[i].Cells[11].FindControl("chkSelect");
//修改状态
chkSelect.Checked = chkSelectAll.Checked;
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//告诉GridView当前显示的数据是第几页的数据
this.GridView1.PageIndex= e.NewPageIndex;
ShowProducts();
}

asp.net操作GridView添删改查的两种方法 及 光棒效果的更多相关文章

  1. GridView 鼠标经过时变色两种方法

    第一种: 新建一个js文件 并引用 <script src="jquery.js" type="text/javascript"></scri ...

  2. 一、winForm-DataGridView操作——控件绑定事件的两种方法

    在winForm窗体中绑定(注册)事件的方法有两种: 一.绑定事件 双击控件,即进入.cs的代码编辑页面,会出现 类似于“ private void 控件名称_Click(object sender, ...

  3. vue通过v-for渲染的列表,可以单独操作的其中的列表的两种方法

    如图,三个标题分别有多个子元素.通过点击三个标题分别控制显示和隐藏.上代码 第一种情况:点击 青1,其所有的标题下的列表全部隐藏,也就是只有一个标题的会显示子元素 <div class=&quo ...

  4. 推断php操作mysql(添删改查)是否成功

    近期在使用CI框架 , 可是里面的数据库操作没有ThinkPhp方便 , 不知道数据库操作的反馈信息 , 仅仅好借助原生方法来推断是否操作数据库成功 推断php操作mysql(添删改查)是否成功,主要 ...

  5. 【ASP.NET MVC系列】浅谈jqGrid 在ASP.NET MVC中增删改查

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  6. 2. MongoDB基本操作 —— 用Mongo.exe操作数据库增删改查

    一.开篇 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象 ...

  7. 使用python操作XML增删改查

    使用python操作XML增删改查 什么是XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类似 HTML XML 的设计宗旨是传输 ...

  8. js操作indexedDB增删改查示例

    js操作indexedDB增删改查示例 if ('indexedDB' in window) { // 如果数据库不存在则创建,如果存在但是version更大,会自动升级不会复制原来的版本 var r ...

  9. MySQL数据分析(16)— 数据操作之增删改查

    前面我们说学习MySQL要从三个层面,四大逻辑来学,三个层面就是库层面,表层面和数据层面对吧,数据库里放数据表,表里放数据是吧,大家可以回忆PPT中jacky的这图,我们已经学完了库层面和表层面,从本 ...

随机推荐

  1. ios 让两个tableView同时处于选中状态

    - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { [_l ...

  2. R语言中的一些函数

    1.控制输出数字的精度 format(123.123,digits=4) 输出4位数字123.1,如果整数超过4位,小数部分就全被略去. options(digits=4) 功能同上,不过在Rsess ...

  3. 160301、js倒计时,页面上显示时间

    js: //倒计时 var countdown=60,t; function settime(){ if (countdown == 0) { $("#validateBtn"). ...

  4. Linux下查看mysql路径

    ps -ef|grep mysql

  5. 第八周课上测试ch03

    测试-1-ch03 任务详情 通过输入gcc -S -o main.s main.c,将下面c程序"week04学号.c"编译成汇编代码 int g(int x){ return ...

  6. LVS,HAPROXY,NGINX各自的优缺点

    Nginx/LVS/HAProxy的基于Linux的开源免费的负载均衡软件. LVS:使用集群技术和Linux操作系统实现一个高性能.高可用的服务器,它具有很好的可伸缩性.可靠性和可管理性,是一款强大 ...

  7. authz_core_module

    w https://httpd.apache.org/docs/trunk/mod/mod_authz_core.html codeigniter index.html .htaccess <I ...

  8. ES6中的let和const

    let和const let 用来声明变量,但是所声明的变量只在let命令所在的代码块内有效 { let a=12 alert(a)//12 } alert(a)//报错 找不到 let不像var那样会 ...

  9. 使用JavaMail发送邮件,465端口开启ssl加密传输

    package com.wangxin.test; import java.security.Security; import java.util.Date; import java.util.Pro ...

  10. 20190401-记录一次bug ConstraintViolationException

    org.hibernate.exception.ConstraintViolationException 违反唯一约束条件 导致这个问题的原因有很多种. 在查询数据库时发生了这样的错误,一般这样的问题 ...