gridview的编辑,更新,取消,自动分页等
gridview编辑列,把左下角的"自动生成字段"的复选框的勾去掉
添加boundfield(绑定列)将其datafield设置为productname,headertext设置为"产品名"
再添加commadfield下的编辑,更新,取消
确定
.cs文件按中编写一绑定gridview数据的方法:
public void bind()
{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=Northwind");
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from products", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
conn.Close();
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
在load事件中调用bind()方法.......
将gridview的datakeynames设置为productid(和datalist的datakeyfield有点像)
1.编辑:
找到gridview的RowEditing事件:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //编辑的行设置为鼠标作用的当前行
bind(); //一定要进行绑定
}
2.更新:找到gridview的RowUpdating事件:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
{
int i = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); //关键代码:取当前选中行产品的id
string productname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text; //关键代码:取编辑框中产品名(改过后的名称),Rows[e.RowIndex].Cells[0].Controls[0]表示当前行,第一列(cell[0])第一个控件(control[0])
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=Northwind");
conn.Open();
string strSQL = "update products set productname='" + productname + "'where productid='" + i + "'";
SqlCommand cmd = new SqlCommand(strSQL, conn);
int h=cmd.ExecuteNonQuery();
if (h > 0)
{
Response.Write("<script>alert('success')</script>");
GridView1.EditIndex = -1; //一定要设置,否则还处于编辑状态
}
else
Response.Write("<script>alert('success')</script>");
bind();
}
}
3.取消:找到gridview的RowCancelingEdit事件:
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind(); //最后还要重新绑定,否则还是呈现"更新"和"取消"
}
4.自动分页:
将gridview的allowpaging设置为true,pagesize自己定义如5
找到gridview的PageIndexChanging事件:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind(); //一定要重新绑定,否则是没效果的
}
5.gridview的rowcommand事件中取主键://模板列中放一button并把他的commandname设置为show
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "show")
{
//GridViewRow drv=((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
//string index = drv.RowIndex.ToString(); //行号
//string admin_id = GridView1.DataKeys[Convert.ToInt32(index)].Value.ToString(); //主键
//Response.Write(index+";"+admin_id);
//下面的方法需要在前台把button的 CommandArgument ='<%# Eval("admin_id")%>'
int admin_id=Convert.ToInt32(e.CommandArgument.ToString()); //主键
int index = Convert.ToInt32(((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent)).RowIndex); //行号
Response.Write(admin_id+";"+index);
}
}
gridview的编辑,更新,取消,自动分页等的更多相关文章
- GridView编辑、取消按钮自定义控件
这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. < ...
- 单击GridView进入编辑模式
一直以来,Insus.NET在实现GridView编辑时,均是在每笔记录第一列或是最后一列放置编辑铵钮,点击编辑铵钮之后,进行编辑模式.本博文是使用另外方式,即是点击GridView记录行任一位置,进 ...
- AngularJS进阶(十一)AngularJS实现表格数据的编辑,更新和删除
AngularJS实现表格数据的编辑,更新和删除 效果 实现 首先,我们先建立一些数据,当然你可以从你任何地方读出你的数据 var app = angular.module('plunker', [' ...
- JasperReports 取消自动分页/忽略分页
因为需要将合同比价单由PDF文档形式改为HTML页面方式,虽然转换文档类型了,但是发现HTML页面中间到了一定行数就出现了空行把Detail给隔开了.之前总想着怎样消除中间空行,以为是报表top页面边 ...
- 【ADO.NET基础-GridView】GridView的编辑、更新、取消、删除以及相关基础操作代码
代码都是基础操作,后续功能还会更新,如有问题欢迎提出和提问....... 前台代码: <asp:GridView ID=" OnRowDataBound="GridView1 ...
- ASP.NET(C#) GridView (编辑、删除、更新、取消)
转自:http://my.oschina.net/dldlhrmc/blog/93458 前台代码 view source print? 01 <%@ Page Language=" ...
- GridView行编辑、更新、取消、删除事件使用方法
注意:当启用编辑button时,点击编辑button后会使一整行都切换成文本框.为了是一行中的一部分是文本框,须要把以整行的全部列都转换成模板,然后删掉编辑模板中的代码.这样就能使你想编辑的列转换成文 ...
- 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
先来看看下面实时效果演示: 用户点击编辑时,在点击行下动态产生一行.编辑铵钮变为disabled.新产生的一行有更新和取消的铵钮,点击“取消”铵钮,删除刚刚动态产生的行.编辑铵钮状态恢复. 更新与删除 ...
- ASP.NET中的GridView自带的编辑更新功能
string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].Connec ...
随机推荐
- 迷你MVVM框架 avalonjs 学习教程19、avalon历史回顾
avalon最早发布于2012.09.15,当时还只是mass Framework的一个模块,当时为了解决视图与JS代码的分耦,参考knockout开发出来. 它的依赖收集机制,视图扫描,绑定的命名d ...
- Invoke,BeginInvoke的作用
这两个方法主要是让给出的方法在控件创建的线程上执行 凡是使用BeginInvoke和Invoke调用的线程都是在UI主线程中执行的
- python selenium点滴
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Ch ...
- 配置MySQL 5.6的主从复制
工具/原料 Windows 7 64位 MySQL 5.6 配置新数据库的主从复制 1 先在主数据库中创建新数据库rep_test. 然后编辑主数据库的my.ini文件 在[mysqld]节点 ...
- hdoj2859(矩阵DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 思路: 第一次碰到这种矩阵上的DP题,想了半天也没想明白.本来想用子矩阵的左上角坐标和右下角坐标 ...
- ubuntu16.04安装virtualbox
download:download.virtualbox.org/virtualbox/5.0.10/virtualbox-5.0_5.0.10-104061~Ubuntu~trusty_amd64. ...
- ubuntu安装谷歌拼音输入法
在这篇教程中,我将告诉你如何在ubuntu系统上安装谷歌拼音输入法.谷歌拼音输入法有基于ibus框架的,也有基于fcitx框架的.我只演示fcitx框架下谷歌拼音输入法的安装,因为ibus框架的谷歌拼 ...
- C & C++ 宏与const
1.宏定义函数: 例:#define do{exp} while(0)与#define exp有什么不同,好处在哪里? 定义复杂代码,防止分号,或是括号不匹配等错误.比如: 定义: #define s ...
- Dice
Dice http://acm.hdu.edu.cn/showproblem.php?pid=5012 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- mysql基本的增删改查和条件语句
增 insert into 表名(列名,列名......) values("test1",23),("test2",23),("test3" ...