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
}
查看全文
相关阅读:
C++多线程基础学习笔记(三)
js 实现排序算法 -- 快速排序(Quick Sort)
js 实现排序算法 -- 归并排序(Merge Sort)
Typescript
Typescript
Typescript
Typescript
Typescript
Typescript
js 实现排序算法 -- 希尔排序(Shell Sort)
原文地址:https://www.cnblogs.com/hubcarl/p/1423540.html
最新文章
Java知识26 多态 (重点巩固)【多测师】
Java知识25 重写Override和重载Overload (重载需要巩固)【多测师】
django-tinymce
FastDFS集群、安装、配置(精简版)
nginx设置开机启动方法
centos查看服务进程端口监听状态
fastdfs常用命令
pycharm 设置文件头部注释
centos静态ip配置
很经典的一段代码-django source code
热门文章
django动态加载模块实例
条件编译之宏开关
C++多线程基础学习笔记(九)
C++多线程基础学习笔记(八)
C++多线程基础学习笔记(七)
C++多线程基础学习笔记(六)
C++多线程基础学习笔记(五)
MySQL笔记
C++多线程基础学习笔记(四)
DLL的创建与使用
Copyright © 2011-2022 走看看