This morning, we bound the infinitus category to the Treeview control. As a result, a recursive algorithm killed me and made the Internet powerful. Thanks to CCTV, MTV, and musicmv.
The following is the table creation code for my database.
Create Table tree
(
Treeid int identity (1, 1) primary key, ---- primary key increments
Treename varchar (50) not null, ---- category name
Treeno int default 0 --- the parent class ID is 0, which indicates the first class.
)
Below is the data in my database
Treeid treename treeno
------------------------------------------------------------------------
1 First Class 0
2 Level 2 Classification 1
3 level 3 Classification 2
Level 4 classification 3
5 second-largest category 0
6 level 2 Classification 5
7 Level 3 Classification 6
8 category 3 0
9 Fourth Class 0
10, 5 levels, 4
11 Level 2 Category 8
12 Level 3 Classification 11
13 Level 2 Classification 9
14 Level 3 Classification 13
Asp.net code
/// <Summary> <br/> // control binding <br/> /// </Summary> <br/> private void treeviewbind () <br/>{< br/> string SQL = "select * From Tree"; <br/> dataset DS = sqlhelper. dataset (sqlhelper. connectionstring, commandtype. text, SQL); // The obtained DT code is called here. If you do not add the code, write it yourself. <br/> dataview DV = Ds. tables [0]. defaultview; <br/> DV. rowfilter = "treeno = 0"; <br/> treeview1.showcheckboxes = treenodetypes. all; <br/> foreach (datarowview DRV in DV) <br/>{< br/> treenode node = new treenode (); <br/> node. TEXT = DRV ["treename"]. tostring (); <br/> node. value = DRV ["treeid"]. tostring (); <br/> node. expanded = false; <br/> treeview1.nodes. add (node); <br/> addreplies (DV, node ); <br/>}</P> <p> // <summary> <br/> // recursively bind a subnode <br/> // /</Summary> <br/> /// <Param name = "DV"> </param> <br/> /// <Param name = "Node"> </param> <br/> private void addreplies (dataview DV, treenode node) <br/>{< br/> DV. rowfilter = "treeno = '" + node. value + "'"; <br/> foreach (datarowview row in DV) <br/>{< br/> treenode replynode = new treenode (); <br/> replynode. TEXT = row ["treename"]. tostring (); <br/> replynode. value = row ["treeid"]. tostring (); <br/> replynode. expanded = false; <br/> node. childnodes. add (replynode); <br/> addreplies (DV, replynode); <br/>}</P> <p>
The effect is as follows: