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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - zqf620

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