树的展现方式

树的展现方式有很多种,我们最常用的就是递归了,但这是我要向大家介绍的是一种比较好的树展现方式:请看下面.

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_code0的表示这是树里面的一个父节点,否则f_parentnode_code为他的父节点的ID,我们看“首页”那一条的f_node_code001001,“行业新闻”是他的子 第二层子节点的第一个节点,那么“行业新闻”的f_node_code001001.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();

 

              }

 

 

 

              ///

1`

 

              /// 构造出树

 

              ///

 

              /// 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();

 

                    

 

                    

 

              }

 

              ///

 

              /// 得到树中选中

 

              ///

 

              /// 操作的树

 

              /// 参数数组

 

              /// 返回一个dataset

 

              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;

 

 

 

              }

 

              ///

 

              /// 得到一个最后一个节点的数据

 

              ///

 

              /// 当前操作的树

 

              /// 一个DataRow

 

              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);

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值