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 ...
随机推荐
- nginx的配置文件解析
worker_processes ;#工作进程的个数,一般与计算机的cpu核数一致 events { worker_connections ;#单个进程最大连接数(最大连接数=连接数*进程数) } h ...
- XML文件的写,集合XML序列化(写)。XML文件的读,递归遍历
XML文件:必须要有一个节点.检验xml文件,可以用浏览器打开,能打开表示对,否则错. 处理方法: DOM:XmlDocument文档对象模型 Sax(事件驱动,XmlReader) XmlSeria ...
- ubuntu 软件包系统已损坏 解决方法
sudo apt-get clean sudo apt-get -f install sudo apt-get upgrade
- 列表中语句 python
找到两个列表中相同元素: list1 = [1,2,3,4,5] list2 = [4,5,6,7,8] print ([l for l in list1 if l in list2]) 输出: [4 ...
- sql重复数据只取一条记录
1.SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法: SEL ...
- HTTP协议图示详解
一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...
- (转) Ringbuffer为什么这么快?
原文地址:http://ifeve.com/ringbuffer/ 最近,我们开源了LMAX Disruptor,它是我们的交易系统吞吐量快(LMAX是一个新型的交易平台,号称能够单线程每秒处理数百万 ...
- 【校招面试 之 剑指offer】第9-1题 用两个栈实现一个队列
#include<iostream> #include<stack> using namespace std; template <typename T> void ...
- SQL某时间段记录查询
--查询当天: select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: select * from info wh ...
- struts框架问题四之获取到值栈的对象
4. 问题四 : 如何获得值栈对象 * 获得值栈对象 有三种方法 * ValueStack vs1 = (ValueStack) ServletActionContext.getRequest().g ...