TreeView recursively binds unlimited data categories, and treeview Recursion

Source: Internet
Author: User

TreeView recursively binds unlimited data categories, and treeview Recursion

TreeView recursively binds unlimited data categories

To implement a dynamic binding, You need to bind the data to the TreeView control when you implement unlimited classification data. The structure of the classification table is as follows:

Field

Type

Id

Int

ParentId

Int

Name

Nvarchar (64)

 

Implement Data Binding:

Private void ControlsDataBind () {tvCategory. nodes. clear (); List <Models. category> types = CommonNews. helper. operateContext. current. loadNewsTypes (); IEnumerable <Models. category> rootNodes = types. where (t => t. parentId = 0); TreeNode node = null; foreach (Models. category item in rootNodes) {node = new TreeNode (item. categoryName, item. categoryId. toString (); if (tvCategory. nodes. contains (node) {continue;} IEnumerable <Models. category> ts = types. where (t => t. parentId = item. categoryId); AddNodesToTree (ts, node, 0) ;}} private void AddNodesToTree (IEnumerable <Models. category> category, TreeNode node, int level) {TreeNode childNode = null; foreach (Models. category c in category) {childNode = new TreeNode (c. categoryName, c. categoryId. toString (); if (tvCategory. nodes. contains (childNode) {continue;} node. childNodes. add (childNode); AddNodesToTree (category. where (t => t. categoryId = c. parentId), childNode, level + 1);} tvCategory. nodes. add (node );}TreeViewBind

 

Related Article

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.