TreeView Recursive binding Unlimited categorical data
To implement a dynamic binding that requires data to be bound to the TreeView control when an infinite class of data is classified, the structure of the classification table is this:
Field |
Type |
Id |
int |
ParentID |
int |
Name |
Nvarchar (64) |
To implement data binding:
Private voidControlsdatabind () {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 Iteminchrootnodes) {Node=NewTreeNode (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 voidAddnodestotree (ienumerable<models.category> Category, TreeNode node,intLevel ) {TreeNode Childnode=NULL; foreach(Models.category Cinchcategory) {Childnode=NewTreeNode (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
TreeView Recursive binding Unlimited categorical data