Populate TreeView from DataBase

前端 未结 6 1693
予麋鹿
予麋鹿 2020-12-02 15:14

I have a database table (named Topics) which includes these fields :

  1. topicId
  2. name
  3. parentId

and by using them I wanna populate

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 15:46

    Not quite.

    Trees are usually handled best by not loading everything you can at once. So you need to get the root node (or topic) which has no parentIDs. Then add them to the trees root node and then for each node you add you need to get its children.

    foreach (DataRow row in topicsWithOutParents.Rows)
    {
       TreeNode node = New TreeNode(... whatever);
       DataSet childNodes = GetRowsWhereParentIDEquals(row["topicId"]);
       foreach (DataRow child in childNodes.Rows)
       { 
           Treenode childNode = new TreeNode(..Whatever);
           node.Nodes.add(childNode);
       }
       Tree.Nodes.Add(node);
    }
    

提交回复
热议问题