zoukankan
html css js c++ java
TreeView数据绑定方法
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.Data .SqlClient;
using
System.Data.OleDb;
public
partial
class
TreeView数据绑定方法 : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
Page.IsPostBack)
{
BindTree();
//
InitTree();
}
}
主从表绑定
#region
主从表绑定
private
void
BindTree()
{
DataSet dst
=
GetTreeViewData();
TreeView1.ShowCheckBoxes
=
TreeNodeTypes.All;
foreach
(DataRow masterRow
in
dst.Tables[
"
province
"
].Rows)
{
TreeNode masterNode
=
new
TreeNode((
string
)masterRow[
"
province
"
]);
TreeView1.Nodes.Add(masterNode);
foreach
(DataRow childRow
in
masterRow.GetChildRows(
"
Children
"
))
{
TreeNode childNode
=
new
TreeNode((
string
)childRow[
"
city
"
]);
masterNode.Expanded
=
false
;
masterNode.ChildNodes.Add(childNode);
}
}
}
private
DataSet GetTreeViewData()
{
//
string constring = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
string
constring
=
System.Configuration.ConfigurationSettings.AppSettings[
"
OleConnection
"
].ToString();
OleDbConnection con
=
new
OleDbConnection(constring);
OleDbDataAdapter daprovince
=
new
OleDbDataAdapter(
"
SELECT * FROM province
"
, con);
OleDbDataAdapter dacity
=
new
OleDbDataAdapter(
"
SELECT * FROM city
"
, con);
DataSet ds
=
new
DataSet();
daprovince.Fill(ds,
"
province
"
);
dacity.Fill(ds,
"
city
"
);
ds.Relations.Add(
"
Children
"
, ds.Tables[
"
province
"
].Columns[
"
provinceid
"
], ds.Tables[
"
city
"
].Columns[
"
father
"
]);
return
ds;
}
#endregion
递归绑定同一个表数据
#region
递归绑定同一个表数据
private
void
InitTree()
{
DataTable dt
=
GetTreeViewTable();
DataView dv
=
new
DataView(dt);
dv.RowFilter
=
"
ParentID=0
"
;
TreeView1.ShowCheckBoxes
=
TreeNodeTypes.All;
foreach
(DataRowView drv
in
dv)
{
TreeNode node
=
new
TreeNode();
node.Text
=
drv[
"
text
"
].ToString();
node.Value
=
drv[
"
ID
"
].ToString();
node.Expanded
=
false
;
TreeView1.Nodes.Add(node);
AddReplies(dt,node);
}
}
private
DataTable GetTreeViewTable()
{
string
constring
=
ConfigurationManager.ConnectionStrings[
"
ConnectionStr
"
].ToString();
OleDbConnection con
=
new
OleDbConnection(constring);
OleDbDataAdapter da
=
new
OleDbDataAdapter(
"
SELECT * FROM treeview
"
, con);
DataTable dt
=
new
DataTable();
da.Fill(dt);
return
dt;
}
private
void
AddReplies(DataTable dt, TreeNode node)
{
DataView dv
=
new
DataView(dt);
dv.RowFilter
=
"
ParentID='
"
+
node.Value
+
"
'
"
;
foreach
(DataRowView row
in
dv)
{
TreeNode replyNode
=
new
TreeNode();
replyNode.Text
=
row[
"
text
"
].ToString();
replyNode.Value
=
row[
"
ID
"
].ToString();
replyNode.Expanded
=
false
;
node.ChildNodes.Add(replyNode);
AddReplies(dt,replyNode);
}
}
#endregion
}
查看全文
相关阅读:
【PHP】window系统中设置计划任务,定时调用某接口
【php】在laravel中使用 easy-wechat实现企业付款到零钱
【转载】laravel中使用WangEditor及多图上传
[PHP] curl: (60) SSL certificate problem: unable to get local issuer certificate
阿里云服务器win10 访问服务器图片资源提示 401
【PHP】创瑞短信接口
C#中Lock锁的对象选择问题
TCP三次握手,四次挥手异常情况(坑)
C# Hashtable、HashSet和Dictionary的区别
浅析C# Dictionary实现原理
原文地址:https://www.cnblogs.com/hubcarl/p/1423540.html
最新文章
nginx tomcat 负载均衡
Tomcat双向证书验证
oracle集合操作
oracle REGEXP_SUBSTR函数
JNDI环境变量
JAAS授权
解决nvcc找不到的问题
pytorch下DeepSpeed安装
pytorch 安装太慢解决办法
torch tensorflow 乘法对比
热门文章
[2017]Bootstrapping Entity Alignment with Knowledge Graph Embedding
[AAAI 2020]Knowledge Graph Alignment Network with Gated Multi-hop Neighborhood Aggregation
python字典互换键值
pycharm调试时出现:Unable to display frame variables
linux无root权限安装screen, screen常用命令
python中的operator.itemgetter函数
PSR-0 自动加载类,简单框架
mysql计算两个时间戳/日期间的工作日,兼容节假日
【linux】lnmp环境安装(源码安装)+ phpMyAdmin安装
【Laravel】find_in_set按指定顺序排序排序
Copyright © 2011-2022 走看看