zoukankan
html css js c++ java
使用DataSet更新GridView中内容
private
void
Bind()
{
SqlConnection con
=
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"
ConnStr
"
].ConnectionString);
SqlDataAdapter sda
=
new
SqlDataAdapter(
"
select * form name
"
,con);
DataSet ds
=
new
DataSet();
sda.Fill(ds,
"
temp
"
);
con.Close();
GridView1.DataSource
=
ds.Tables[
"
temp
"
].DefaultView;
GridView1.DataBind();
}
private
void
fill(
int
id,
string
name,
int
age)
{
SqlConnection con
=
new
SqlConnection(ConfigurationManager.ConnectionStrings
[
"
ConnStr
"
].ConnectionString);
SqlDataAdapter sda
=
new
SqlDataAdapter(
"
select * from name
"
, con);
SqlCommandBuilder scbld
=
new
SqlCommandBuilder(sda);
//
如果没看上面这句,那DataSet将只能Selete不能Update
DataSet ds
=
new
DataSet();
try
{
sda.Fill(ds,
"
temp
"
);
ds.Tables[
"
temp
"
].DefaultView.Sort
=
"
id
"
;
//
按id排序
int
index
=
ds.Tables[
"
temp
"
].DefaultView.Find(id);
//
找到我们要的数据所在行的索引
ds.Tables[
"
temp
"
].Rows[index][
"
name
"
]
=
name;
ds.Tables[
"
temp
"
].Rows[index][
"
age
"
]
=
age;
//
更新DataSet里面的数据必须使用数组的方式。
int
rows
=
sda.Update(ds,
"
temp
"
);
Response.Write(
"
成功更新了
"
+
rows
+
"
行数据
"
);
}
catch
(Exception e)
{
Response.Write(
"
出现错误原因是:
"
+
e.Message);
}
}
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
{
int
index
=
e.RowIndex;
int
id
=
Convert.ToInt32(GridView1.Rows[index].Cells[
1
].Text);
//
上面是说GridView1的行号要动态取,而列号是固定的
string
name
=
((TextBox)GridView1.Rows[index].Cells[
2
].FindControl(
"
TextBox1
"
)).Text;
int
age
=
Convert.ToInt32(((TextBox)GridView1.Rows[index].Cells[
3
].FindControl(
"
TextBox2
"
)).Text);
fill(id,name,age);
GridView1.EditIndex
=
-
1
;
Bind();
}
查看全文
相关阅读:
web前端攻城狮都来晒一晒你的收藏夹吧
淘宝前端技术系列课程分享
HTML5编程实战之二:用动画的形式切换图片
HTML5编程实战之一:HTML5时钟
【转】chrome developer tool 调试技巧
Android 云端推送C2DM php实现向终端推送消息
简单的泰国IP判断
[翻译]延迟着色(Shawn Hargreaves)〔1〕
[翻译]延迟着色(2)
[3D基础]投影矩阵的推导(1)
原文地址:https://www.cnblogs.com/yeagen/p/1330926.html
最新文章
C# 去掉HTML标记的正则表达式
vim使用笔记(2): NERDtree插件
试析J2EE与.NET时代的商业利润
Memcache使用基础
《大规模 web服务开发》笔记
3月10日学习小结
开博第一篇
MySQL的正则表达式
Ecshop首页购物车数量调取问题
jQuery总结第一篇
热门文章
关于VIM编码问题的解决
Cron笔记
jsp ftp上传下载实例
C# 3.5新特性整理
SQL的语法和规则
C#XmlDocument,XDocument互换
SQL语句大全
C#调用Java类的方法
生成二维码
HTML5编程实战之三:图片文本(txt)拖拽预览
Copyright © 2011-2022 走看看