惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
腾讯CDC
P
Privacy & Cybersecurity Law Blog
V
Vulnerabilities – Threatpost
小众软件
小众软件
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
P
Privacy International News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
Forbes - Security
Forbes - Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
Google Developers Blog
S
Secure Thoughts
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
TaoSecurity Blog
TaoSecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Last Week in AI
Last Week in AI
H
Heimdal Security Blog
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
Security Latest
Security Latest
SecWiki News
SecWiki News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Security Affairs
V2EX - 技术
V2EX - 技术
S
Schneier on Security
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
博客园_首页
AI
AI
Help Net Security
Help Net Security
I
Intezer
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
D
Docker

博客园 - zqf620

.Net PetShop 3.0中购物车总价计算的bug .NET PetShop 3.0 FAQ novalidate选项无效的问题 .NET方向高级开发人员面试时应该事先考虑的问题 (zt) UML中的图 从一组数中每次抽取出一个数,并规定了每个数出现的概率 从一个表中随即抽取100条记录 PL/SQL User's Guide and Reference, Release 2 (9.2) chm版 下载 Oracle中实现自动增长列 在开发过程中运用UML 输出到html页面的字符串的格式化 在DataGrid控件中获取数据项中各列的数据内容 DataGrid控件的分页 DTD简介 W3C XML Schema (XSD) XML相关技术概览 将web窗体页文件(test.aspx)转换成用户控件文件(test.ascx) access作为后台数据库遇到的访问权限问题 HTML实体
在DataGrid控件中编辑数据项
zqf620 · 2007-01-20 · via 博客园 - zqf620

要想在DataGrid控件中编辑数据,请使用"按钮列"中的"编辑、更新、取消"列,这些都可以在DataGrid控件的属性生成器中设置

当为DataGrid控件(以控件名为dg1为例)中加入了"编辑、更新、取消"列后,在页面的dg1控件中会多出一列,该列的每一项都是文本为"编辑"的LinkButton/Button。

如果单击了某一行的"编辑"按钮,则该行处于编辑模式,"编辑"按钮被替换为"更新"和"取消"按钮,该行中所有其它的非只读的数据帮定列都会变成TextBox控件格式,以便用户来编辑修改。
当用户修改了非只读的数据帮定列的数据(在TextBox控件中),单击"更新"按钮,将新值保存(一般是保存到数据库中),单击"取消"按钮,该行退出编辑模式。

为了达到单击"编辑"按钮,就转换为行编辑模式的效果,必须编写dg1的EditCommand事件处理方法
为了达到单击"更新"按钮,就保存新值的效果,必须编写dg1的UpdateCommand事件处理方法
为了达到单击"取消"按钮,就退出行编辑模式,必须编辑dg1的CancelCommand事件处理方法

1) dg1.EditCommand事件处理方法-进入行的编辑模式
------------------------------------------------------
private void dg1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
  dg1.EditItemIndex = e.Item.ItemIndex; //设置要编辑的项的索引
  binddg1();  //为dg1绑定数据的方法。设置要编辑的项后,要求重新绑定dg1
}
---------------------------------------------------------

2) dg1.CancelCommand事件处理方法-退出行的编辑模式
---------------------------------------------------------
private void dg_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
  dg1.EditItemIndex = e.Item.ItemIndex; //编辑的项的索引为-1,就是不编辑任何项
  binddg1();  //重设EditItemIndex后,要求重新绑定dg1
}
---------------------------------------------------------

3) dg1.UpdateCommand事件处理方法-保存更新了的值
要保存更新了的值,首先要重页面中获取这些新值。在事件处理方法中,主要要实现三个功能:
获取更新了的值、更新这些值、退出行更新模式。

一般,DataGrid控件中的显示的数据都是从数据库表中取得的,所以更新了的值也要保存到数据库中,可以用一条update Sql语句或存储过程来执行更新。

要从页面中获取处于编辑模式的行中各列的值,需要一些技巧。以绑定列为例:
获取只读绑定列的值:  e.Item.Cells[列索引].Text  //只读帮定列处于非编辑状态
获取非只读绑定列的值:((TextBox)(e.Item.Cells[列索引].Controls[0])).Text //处于编辑状态

只读帮定列通常是表中的主键列,其列值要用在update语句的where子句中,一般不更新它们

代码示例:
----------------------------------------------------------
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
  //只读绑定列,处于非编辑状态
  string customerid = e.Item.Cells[1].Text; 
  //非只读绑定列,处于编辑状态
  string companyname = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;
  string city = ((TextBox)(e.Item.Cells[3].Controls[0])).Text;

  String strSql = "update customers set companyname = '" + companyname +
 "',city = '" + city + "' where customerid = '" + customerid + "'";
  executeSql(strSql);  //执行update语句,进行更新

  DataGrid1.EditItemIndex = -1;     //退出行的编辑模式
  binddg1();
}
--------------------------------------------------------------