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();
}
查看全文
相关阅读:
python常用函数年初大总结
Linux系统巡检常用命令
码率
视频码率计算问题
Python快速教程
MFC消息机制
MySQL多表查询
VoIP的话音质量测量方法
用Py2exe打包Python脚本简单介绍
在Eclipse中执行Andorid test preject提示The connection to adb is down, and a severe error has occured.解决方法
原文地址:https://www.cnblogs.com/yeagen/p/1330926.html
最新文章
git提交本地分支到远程分支(转)
低功耗板子测试功耗的方法(转)
说说单片机的裸奔程序的框架(转)
ARM内核全解析(转)
Ethernet(以太网)之一 详解 MAC、MII、PHY
STM32 之二 HAL库详解 及 手动移植
STM32 之八 在线升级(IAP)
MCU之芯唐ARM9
关于ARM的内核架构
嵌入式系统UBOOT
热门文章
Python中pip安装问题解决
python常用正则表达式
django搭建Bootstrap常用问题解决方法
使用PyInstaller打包Python程序
python发送各类邮件的主要方法
Python编码介绍——encode和decode
内存泄露分析
WinDbg抓取程序报错dump文件的方法
Apache与Tomcat 区别联系
VMware 8安装苹果操作系统Mac OS X 10.7 Lion正式版
Copyright © 2011-2022 走看看