樹的展現方式有很多種,我們最常用的就是遞歸了,但這是我要向大家介紹的是一種比較好的樹展現方式:請看下面.
CREATE TABLE [dbo].[t_flow_treeview] (
[f_flowtreeview_ID] [int] IDENTITY (1, 1) NOT NULL ,
[f_node_code] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[f_node_name] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[f_parentnode_code] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[f_parentnode_name] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
[f_recommendinfo_NUM] [int] NULL ,
[f_link_url] [varchar] (200) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
實際數據如下:(部分與構造樹無關的字段略掉)
f_flowtreeview_ID |
f_node_code |
f_node_name |
f_parentnode_code |
|
97 |
001001 |
首頁 |
0 |
10 |
98 |
001001.002001 |
行業新聞 |
001001 |
10 |
99 |
001001.002002 |
招標公告 |
001001 |
9 |
100 |
001001.002003 |
政務信息 |
001001 |
20 |
101 |
001001.002004 |
項目信息 |
001001 |
10 |
102 |
001001.002005 |
中標結果 |
001001 |
10 |
103 |
001001.002006 |
資質公告 |
001001 |
10 |
104 |
001001.002007 |
政策文件 |
001001 |
12 |
105 |
001001.002008 |
行業信息 |
001001 |
10 |
106 |
001002 |
新聞 |
0 |
10 |
107 |
001002.002001 |
推薦信息 |
001002 |
20 |
108 |
001002.002002 |
精彩專題 |
001002 |
20 |
109 |
001002.002003 |
每日聚焦 |
001002 |
10 |
110 |
001003 |
政務信息 |
0 |
10 |
111 |
001003.002001 |
政策文件 |
001003 |
100 |
112 |
001003.002002 |
熱點關注 |
001003 |
100 |
113 |
001004 |
項目信息 |
0 |
10 |
114 |
001004.002001 |
綜合信息 |
001004 |
14 |
115 |
001004.002002 |
物資信息 |
001004 |
14 |
116 |
001005 |
建設物質 |
0 |
10 |
大家可以自己分析一下上面各數據之間的關係,f_parentnode_code爲0的表示這是樹裏面的一個父節點,否則f_parentnode_code爲他的父節點的ID,我們看“首頁”那一條的f_node_code爲001001,“行業新聞”是他的子 第二層子節點的第一個節點,那麼“行業新聞”的f_node_code爲001001.002001 ,前6位表示父節點的ID,後6位表示第二層的第一個節點,類推,第二層第3個是 001001.002003,第三層第一個子節點是001001.003001,我想大概你已經理解了
*********************************************************
代碼
*********************************************************
using System;
using Microsoft.Web.UI.WebControls;
namespace CreateTree
{
///
/// Class1 的摘要說明。
///
public class Tree
{
protected string _NodeCodeColumnName;//節點代碼在數據表中的列名
protected string _NodeNameColumnName;//節點名在數據表中的列名
protected string _ParentCodeColumnName;//節點的父節點id在表數據表中的列名
protected System.Collections.Hashtable _hashTable;//
protected string _TableName;//構成樹的表名
protected System.Data.DataSet dataSet;//構成樹的數據集合
protected System.Data.SqlClient.SqlConnection sqlConnection;
protected System.Data.SqlClient.SqlCommand sqlCommand;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter;
protected string _sqlConnectionString;//數據庫連接字符串
public Tree()
{
//
// TODO: 在此處添加構造函數邏輯
//
this._hashTable=new System.Collections.Hashtable();
this.dataSet=new System.Data.DataSet();
this.sqlCommand=new System.Data.SqlClient.SqlCommand ();
this.sqlConnection=new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter=new System.Data.SqlClient.SqlDataAdapter();
this.sqlDataAdapter.SelectCommand=this.sqlCommand;
this.sqlCommand.Connection=this.sqlConnection;
}
public string sqlConnectionString
{
get
{
return this._sqlConnectionString;
}
set
{
this._sqlConnectionString=value;
}
}
///
/// 得到或設置節點代碼在數據表中的列名
///
public string NodeCodeColumnName
{
get
{
return this._NodeCodeColumnName ;
}
set
{
this._NodeCodeColumnName=value;
}
}
///
/// 得到或設置構成樹的這張表的表名
///
public string TableName
{
get
{
return this._TableName;
}
set
{
this._TableName=value;
}
}
///
/// 得到或設置節點名在數據表中的列名
///
public string NodeNameColumnName
{
get
{
return this._NodeNameColumnName;
}
set
{
this._NodeNameColumnName=value;
}
}
///
/// 得到或設置節點的父節點代碼在數據表中的列名
///
public string ParentCodeColumnName
{
get
{
return this._ParentCodeColumnName;
}
set
{
this._ParentCodeColumnName=value;
}
}
///
/// 打開數據庫
///
private void OpenDB()
{
if(this.sqlConnection.State==System.Data.ConnectionState.Closed)
{
this.sqlConnection.ConnectionString=this.sqlConnectionString;
this.sqlConnection.Open();
}
}
///
/// 關閉數據庫
///
private void CloseDB()
{
if(this.sqlConnection.State==System.Data.ConnectionState.Open)
{
this.sqlConnection.Close();
}
}
///
/// 構造DataSet
///
protected void CreateDataSet()
{
string strSql="SELECT "+this.NodeCodeColumnName+", "+this.NodeNameColumnName+", "+this.ParentCodeColumnName+" FROM "+this.TableName+" "+
"ORDER BY "+this.NodeCodeColumnName;
this.sqlCommand.CommandType=System.Data.CommandType.Text;
this.sqlCommand.CommandText=strSql;
this.OpenDB();
this.sqlDataAdapter.Fill(this.dataSet);
this.CloseDB();
}
///
/// 構造出樹
///
/// TreeView 控件
public void CreateTree(Microsoft.Web.UI.WebControls.TreeView tree)
{
CreateDataSet();//構造數據集合
foreach(System.Data.DataRow dataRow in this.dataSet.Tables[0].Rows)
{
TreeNode treeNode=new TreeNode();
treeNode.Text=dataRow[this.NodeNameColumnName].ToString();
treeNode.NodeData=dataRow[this.NodeCodeColumnName].ToString();
if(dataRow[this.ParentCodeColumnName].ToString().Equals("0"))
{
tree.Nodes.Add(treeNode);
}
else
{
TreeNode newTreeNode=((TreeNode)this._hashTable[dataRow[this.ParentCodeColumnName]]);
if(newTreeNode!=null)
newTreeNode.Nodes.Add(treeNode);
}
this._hashTable.Add(dataRow[this.NodeCodeColumnName],treeNode);
}
this._hashTable.Clear();
}
///
/// 得到樹中選中
///
/// 操作的樹
/// 參數數組
///
public System.Data.DataSet GetData(Microsoft.Web.UI.WebControls.TreeView treeView,params string[] strArray)
{
string nodedata=treeView.GetNodeFromIndex(treeView.SelectedNodeIndex).NodeData;
string strJoinArray=String.Join(",",strArray);
string strSql="SELECT "+strJoinArray+" FROM "+this.TableName
+" WHERE ("+this.NodeCodeColumnName+" = '"+nodedata+"')";
this.sqlCommand.CommandType=System.Data.CommandType.Text;
this.sqlCommand.CommandText=strSql;
this.OpenDB();
this.sqlDataAdapter.Fill(this.dataSet);
this.CloseDB();
return this.dataSet;
}
///
/// 得到一個最後一個節點的數據
///
/// 當前操作的樹
///
public System.Data.DataRow GetLastNodeData(Microsoft.Web.UI.WebControls.TreeView treeView,params string[] strArray)
{
string nodedata=treeView.GetNodeFromIndex(treeView.SelectedNodeIndex).NodeData;
string strJoinArray=String.Join(",",strArray);
string strSql="SELECT "+strJoinArray+" FROM "+this.TableName
+" WHERE ("+this.NodeCodeColumnName+" = '"+nodedata+"')";
string strCountSql="select count(*) from "+this.TableName+" where "+this.ParentCodeColumnName+"='"+nodedata+"'";
this.sqlCommand.CommandType=System.Data.CommandType.Text;
this.sqlCommand.CommandText=strCountSql;
this.OpenDB();
if(0==(int)this.sqlCommand.ExecuteScalar())
{
this.sqlCommand.CommandText=strSql;
this.sqlDataAdapter.Fill(this.dataSet);
this.CloseDB();
return this.dataSet.Tables[0].Rows[0];
}
else
{
return null;
}
}
}
}
用法:
CreateTree.Tree tree=new CreateTree.Tree();
tree.sqlConnectionString=@"data source=HANCHAOHANCHAO;initial catalog=pubs;persist security info=False;user id=sa;workstation id=HANCHAO;packet size=4096";
tree.TableName="treetable";
tree.NodeCodeColumnName="nodecode";
tree.NodeNameColumnName="nodename";
tree.ParentCodeColumnName="parentcode";
tree.CreateTree(this.TreeView1);