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();
}
查看全文
相关阅读:
Zabbix监控系统系列之二 : 初始化配置
docker中的zabbix 配置钉钉机器人
sqldbx 绿色小巧的数据库查询管理工具
Windows 10 自建Anki 私有云服务器
Sqlserver内存管理:限制最大占用内存
tfs强制撤销其他工作区挂起更改,删除工作区
easyui的一些文档
IIS下日志分析
zookeeper-client
Windbg程序调试系列-索引篇(转)
原文地址:https://www.cnblogs.com/yeagen/p/1330926.html
最新文章
ios 拖线没有反应
iOS添加外部图片
用c#每日更换“必应背景图片”为“桌面壁纸”
LeetCode:缺失数字
LeetCode:有效的括号
解决ASP.NET MVC返回的JsonResult 中 日期类型数据格式问题,和返回的属性名称转为“驼峰命名法”和循环引用问题
Bootstrap Blazor 组件库
netcore webapi参数
Node.js安装及环境配置之Windows篇
WPF中使用代码触发按钮事件。
热门文章
WPF—通过华视电子身份证识别仪读取身份证信息
ATM实现扫描二维码打印(三)PDF静默打印
ATM实现扫描二维码打印(二)下载文件到根目录,并判断文件是PNG还是PDF
ATM实现扫描二维码打印(一)二维模组(扫描枪)识别二维码
通过明华读卡器读取IC卡的数据,并将数据通过HTTP传递
WPF-生成二维码(条码)
C# 定时器简单介绍
WPF实现消息提醒(广告弹窗)
WPF 输入框正则效验
C#操作MongoDB的帮助类-简单封装
Copyright © 2011-2022 走看看