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 ...
随机推荐
- 【337】Text Mining Using Twitter Streaming API and Python
Reference: An Introduction to Text Mining using Twitter Streaming API and Python Reference: How to R ...
- redmine邮件配置
网上找了半天,有很多答案,最后自己测试找出一个解决办法. 1.找到安装位置 D:\Bitnami\redmine-2.5.2-2\apps\redmine\htdocs\config下的文件confi ...
- .Spark Streaming(上)--实时流计算Spark Streaming原理介
Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍 http://www.cnblogs.com/shishanyuan/p/474 ...
- Swagger与SpringMVC整合
依赖管理 在整合之前,需要把所有使用到的依赖包全部引入.网上很多文章只是简单告诉读者引入swagger-springmvc-1.0.2.jar包,但是随后你发现这远远不够,还需要很多包,如下所示: ...
- java.lang.Error: Unresolved compilation problem: 解决方案
严重: Allocate exception for servlet WX_Interfacejava.lang.Error: Unresolved compilation problem: The ...
- C++ auto
auto用来声明自动变量.它是存储类型标识符,表明变量(自动)具有本地范围.块范围的变量声明(如for循环体内的变量声明)默认为auto存储类型. 好处:auto变量在离开作用域是会变程序自动释放,不 ...
- Inside Triangle
Inside Triangle https://hihocoder.com/contest/hiho225/problem/1 时间限制:10000ms 单点时限:1000ms 内存限制:256MB ...
- 判断Javascript对象是否为空
判断普通javascript对象是否为空(含有可枚举的属性,自有的.继承的都可以),可使用jQuery 3.2.1版的isEmptyObject()方法: isEmptyObject: functio ...
- PyQt5信号与槽
简单使用 可以使用designer的一个模式定义,也可以自己定义,在__init__函数里,self.btn.clicked.connect(self.text.clear). 注意:槽不用加括号,可 ...
- 【校招面试 之 C/C++】第25题 C++ 智能指针(一)之 auto_ptr
1.智能指针背后的设计思想 我们先来看一个简单的例子: void remodel(std::string & str) { std::string * ps = new std::string ...