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

推荐订阅源

S
Schneier on Security
A
Arctic Wolf
S
Security Affairs
O
OpenAI News
SecWiki News
SecWiki News
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
N
News | PayPal Newsroom
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
The Hacker News
The Hacker News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
人人都是产品经理
人人都是产品经理
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
The Cloudflare Blog
N
News and Events Feed by Topic
量子位
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Webroot Blog
Webroot Blog

博客园 - 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();
}
--------------------------------------------------------------