Effect of binding a Treeview control to an infinitus category

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.