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();
}
查看全文
相关阅读:
最长递增长度 (最长上升子序列)
完全背包问题
vue中使用el-tabs组件遇到的问题
ORACLE中排序的时候空值处理
ORA-01089数据库无法正常关闭
Oracle中的LPAD和RPAD的使用
Oracle中Translate函数的使用
通过对照表快速建view
Oracle数据库create or replace
打字网站
原文地址:https://www.cnblogs.com/yeagen/p/1330926.html
最新文章
文献管理与信息分析(2018年秋 第九次开课)第三次课程小结
文献管理与信息分析第二次课程小结
文献管理与信息分析第一次课程小结
总线:
磁盘结构与参数
存储器的分类:随机存取存储器与只读存储器
层次化存储结构
流水线周期与流水线执行时长、流水线吞吐率计算、流水线的加速比
计算机结构
计算机中的浮点数表示和运算
热门文章
数据的表示(原码、反码、补码、移码)
进制的互相转换
F-跳跃 牛客假日团队赛2
hdu 1002 A + B Problem II (大数加法)
hdu 2093 考试排名 (模拟)
Codeforces Round #565 (Div. 3) E. Cover it!
Codeforces Round #565 (Div. 3) C. Lose it!
Codeforces Round #565 (Div. 3) B. Merge it!
Codeforces Round #565 (Div. 3) A. Divide it!
hdu 1159 Common Subsequence (最长公共子序列)
Copyright © 2011-2022 走看看