There are many ways to show a lot, and we are most common to recursive, but this is what I want to introduce you is a better tree show: please see below.
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_recomendinfo_num] [int] NULL,
[f_link_url] [varchar] (200) Collate Chinese_prc_ci_as null
) On [primary]
The actual data is as follows: (some of the fields unrelated to the construction tree)
f_flowtreeview_id
f_node_code
f_node_name
f_parentnode_code
97
001001
Homepage
0
10
98
001001.002001
Industry news
001001
10
99
001001.002002
Tender notice
001001
9
100
001001.002003
Government information
001001
20
101
001001.002004
Project information
001001
10
102
001001.002005
Win bid
001001
10
103
001001.002006
Qualification announcement
001001
10
104
001001.002007
Policy documents
001001
12
105
001001.002008
Industry Information
001001
10
106
001002
news
0
10
107
001002.002001
Recommended information
001002
20
108
001002.002002
Exciting topic
001002
20
109
001002.002003
Daily focus
001002
10
110
001003
Government information
0
10
111
001003.002001
Policy documents
001003
100
112
001003.002002
focus
001003
100
113
001004
Project information
0
10
114
001004.002001
General Information
001004
14
115
001004.002002
Material information
001004
14
116
001005
Construct a substance
0
10
Everyone can analyze the relationship between the above data, and f_parentnode_code is 0 means that this is a parent node inside, otherwise f_parentnode_code is his parent node ID, we look at "Home" The f_node_code is 001001, " Industry News "is the first node of his second layer node, then" Industry News "f_Node_code is 001001.002001, the first 6 digits represent the ID of the parent node, and then 6 represent the first node of the second layer, The third layer of the second layer is 001001.002003, the third layer first child node is 001001.003001, I think you have already understood the ******************************* ********************************* Code ************* ******************************************************************** USING SYSTEM; Using Microsoft. Web.ui.WebControls;
Namespace CreateTree
{
///
/// Class1 summary description.
///
Public Class Tree
{
protected string _nodecodecolumnname; // node code column name in the data table
protected string _nodenameColumnname; // Name name name in the data table
Protected string _parentCodeColumnName; // Node's parent node ID in the table data table
protected system.collections.hashtable_hashtable; //
Protected string _tablename; // constitute a table name
Protected system.data.dataset dataset; // Mechanic data collection
protected system.data.sqlclient.sqlconnection sqlconnection;
protected system.data.sqlclient.sqlcommand sqlcommand;
Protected system.data.sqlclient.sqldataadapter sqldataadapter;
protected string _sqlconnectionstring; // Database connection string
Public tree ()
{
//
// TODO: Add constructor logic here
//
THIS._HASHTABLE = New System.collections.hashtable ();
This.dataset = new system.data.dataset ();
This.sqlcommand = new system.data.sqlclient.sqlcommand ();
THIS.SQLCONNECONNECTION = 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;
}
}
///
// get or set the column code in the data table
///
Public string nodecodecolumnname {
get
{
Return this._nodecodecolumnname;
}
set
{
THIS._NODECODECOLUMNNAME = VALUE;
}
}
///
/// Get or set the table name of this table that makes the tree
///
Public String TableName
{
get
{
Return this._tablename;
}
set
{
THIS._TABLENAME = VALUE;
}
}
///
/// get or set the column name in the data table
///
Public string nodenamecolumnname
{
get
{
Return this._nodenamecolumnname;
}
set
{
THIS._NODENAMECOLUMNAME = VALUE;
}
}
///
/// Get or set the column name of the parent code of the node in the data table
///
Public String ParentcodecolumnName
{
get
{
Return this._parentcodecolumnname;
}
set
{
THIS._PARENTCODECOLUMNNAME = VALUE;
}
}
///
/// Open the database
///
Private void Opendb ()
{
IF (this.sqlconnection.state == system.data.connectionState.closed)
{
THIS.SQLCONNECTION.CONNectionstring = this.sqlconnectionstring;
THIS.SQLCONNECTION.OPEN ();
}
}
///
/// Turn the database
///
Private void closedb ()
{
IF (this.sqlconnection.state == system.data.connectionState.Open)
{
THIS.SQLCONNECTION.CLOSE ();
}
}
///
/// Construct DataSet
///
protected void createDataSet ()
{
String strsql = "SELECT" this.nodecodecolumnname "," this.nodenamecolumnname "," this.parentcodecolumnname " 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`
/// Construct the tree
///
/// TreeView control
Public void CreateTree (Microsoft.Web.ui.WebControls.treeView Tree)
{
CreatedataSet (); // Constructive data collection
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 ();
}
///
/// Get in the tree
///
/// Operation tree
/// parameter array
///
Return a DataSet
Public system.data.dataset getdata (Microsoft.Web.ui.WebControls.treeView TreeView, params string [] stratay)
{
String nodedata = treView.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;
}
///
// get the data of a last node
///
/// currently operating tree
///
A DATAROW
Public system.data.datarow getlastnodedata (Microsoft.Web.ui.WebControls.treeView TreeView, params string [] stratay)
{
String nodedata = treView.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;
}
}
}
}
usage:
CreateTree.tree Tree = new createtree.tree ();
Tree.sqlconnectionstring = @ "data source = hanchaohanchao; initial catalog = pubs; persist security info = false; user ID = sa; workstation id = 4096;
Tree.tablename = "treetable";
Tree.NodecodeColumnName = "nodecode";
Tree.NodeNameColumnName = "nodename";
Tree.parentcodecolumnname = "parentcode";
Tree.createtree (this.treeView1);