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

推荐订阅源

T
Tailwind CSS Blog
博客园 - Franky
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 叶小钗
罗磊的独立博客
The GitHub Blog
The GitHub Blog
H
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler
Spread Privacy
Spread Privacy
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
N
Netflix TechBlog - Medium
T
The Exploit Database - CXSecurity.com
云风的 BLOG
云风的 BLOG
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
Cyberwarzone
Cyberwarzone
T
Threatpost
T
Threat Research - Cisco Blogs
Recent Announcements
Recent Announcements
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
量子位
V
V2EX
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Proofpoint News Feed
I
Intezer
Schneier on Security
Schneier on Security
雷峰网
雷峰网
B
Blog RSS Feed
Jina AI
Jina AI
爱范儿
爱范儿
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks 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();
}
--------------------------------------------------------------