zoukankan
html css js c++ java
DataTable应用
code
using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public
partial
class
DataTable应用 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
btnCreate_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
new
DataSet(
"
myDS
"
);
//
创建数据集
DataTable dtMaster
=
new
DataTable(
"
Master
"
);
//
创建数据表
DataTable dtChild
=
new
DataTable(
"
Child
"
);
dsUntyped.Tables.Add(dtMaster);
//
把数据表添加到数据集中
dsUntyped.Tables.Add(dtChild);
Session[
"
ds
"
]
=
dsUntyped;
}
protected
void
btnAddColumn_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
dsUntyped.Tables[
"
Master
"
].Columns.Add(
"
MasterID
"
,
typeof
(
int
));
dsUntyped.Tables[
"
Master
"
].Columns.Add(
"
MasterValue
"
,
typeof
(
string
));
dsUntyped.Tables[
"
Child
"
].Columns.Add(
"
MasterLink
"
,
typeof
(
int
));
dsUntyped.Tables[
"
Child
"
].Columns.Add(
"
ChildID
"
,
typeof
(
int
));
dsUntyped.Tables[
"
Child
"
].Columns.Add(
"
ChildValue
"
,
typeof
(
string
));
//
修改表头
dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterID
"
].Caption
=
"
主ID
"
;
dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterValue
"
].Caption
=
"
值
"
;
Session[
"
ds
"
]
=
dsUntyped;
Bind();
}
protected
void
btnAddRow_Click(
object
sender, System.EventArgs e)
{
try
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
//
为Master表添加两行
DataRow dr
=
dsUntyped.Tables[
"
Master
"
].NewRow();
dr[
"
MasterID
"
]
=
1
;
dr[
"
MasterValue
"
]
=
"
One
"
;
dsUntyped.Tables[
"
Master
"
].Rows.Add(dr);
dr
=
dsUntyped.Tables[
"
Master
"
].NewRow();
dr[
"
MasterID
"
]
=
2
;
dr[
"
MasterValue
"
]
=
"
Two
"
;
dsUntyped.Tables[
"
Master
"
].Rows.Add(dr);
//
为child表添加1行
dr
=
dsUntyped.Tables[
"
Child
"
].NewRow();
dr[
"
MasterLink
"
]
=
1
;
dr[
"
ChildID
"
]
=
1
;
dr[
"
ChildValue
"
]
=
"
ChildOne
"
;
dsUntyped.Tables[
"
Child
"
].Rows.Add(dr);
Session[
"
ds
"
]
=
dsUntyped;
Bind();
}
catch
(Exception ee)
{
Response.Write(ee.Message);
}
}
//
添加唯一键
protected
void
Button1_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
//
表示对一组列的限制,列中所有值必须是唯一的
System.Data.UniqueConstraint uc
=
new
UniqueConstraint(
"
unqi
"
, dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterID
"
]);
dsUntyped.Tables[
"
Master
"
].Constraints.Add(uc);
Session[
"
ds
"
]
=
dsUntyped;
}
private
void
Bind()
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
dgMaster.DataSource
=
dsUntyped.Tables[
"
Master
"
].DefaultView;
dgChild.DataSource
=
dsUntyped.Tables[
"
Child
"
].DefaultView;
this
.DataBind();
}
protected
void
btnAddForeign_Click(
object
sender, System.EventArgs e)
{
//
添加外键
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
System.Data.ForeignKeyConstraint fc
=
new
ForeignKeyConstraint(
"
fc
"
, dsUntyped.Tables[
"
Master
"
].Columns[
"
MasterID
"
], dsUntyped.Tables[
"
Child
"
].Columns[
"
MasterLink
"
]);
dsUntyped.Tables[
"
Child
"
].Constraints.Add(fc);
Session[
"
ds
"
]
=
dsUntyped;
}
protected
void
btnUpdateMID_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
dsUntyped.Tables[
"
Master
"
].Rows[
0
][
"
MasterID
"
]
=
4
;
Bind();
}
protected
void
Button2_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
int
nIndexTb
=
int
.Parse(ddlTable.SelectedItem.Value);
int
nIndexRow
=
int
.Parse(tbRow.Text);
int
nIndexCol
=
int
.Parse(tbCol.Text);
object
obj
=
dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol];
tbResult.Text
=
obj.ToString();
}
protected
void
btnUpdateDs_Click(
object
sender, System.EventArgs e)
{
DataSet dsUntyped
=
(DataSet)Session[
"
ds
"
];
int
nIndexTb
=
int
.Parse(ddlTable.SelectedItem.Value);
int
nIndexRow
=
int
.Parse(tbRow.Text);
int
nIndexCol
=
int
.Parse(tbCol.Text);
dsUntyped.Tables[nIndexTb].Rows[nIndexRow][nIndexCol]
=
tbResult.Text;
Session[
"
ds
"
]
=
dsUntyped;
Bind();
}
}
查看全文
相关阅读:
css 字体相关属性的设置
flex 三列布局
python shelve模块
python collections模块
Python shutil模块,高级文件管理
python中os模块的常用
python中hashlib加密模块和sys系统模块
logging日志模块
random模块常用方法
序列化json和pickle模块
原文地址:https://www.cnblogs.com/hubcarl/p/1423536.html
最新文章
STL各个数据结构特点
输入输出电阻
linux 从一台机器复制文件到另一台linux机器上去
linux 用户权限
Ztree取消选中的所有节点
通过ping命令查看服务器是linux还是windows系列
CMD命令
Linux下安装Maven
Jenkins忘记密码解决方案
maven 命令
热门文章
jquery中的$.fn的用法
理解javascript中的立即执行函数(function(){})()
数组去重
函数总结
数据类型的检测
选项卡
隔行变色
JS的数据类型
Number 数据类型
CSS 文本样式
Copyright © 2011-2022 走看看