简体   繁体   中英

ASP.NET Treeview From Path

I have a data hierarchy path separated by '/', currently in a SQL Server 2008 table. How can I display this path as a TreeView in my ASP.NET C# application.

Here is an example of what the table looks like:

Parent1/
Parent2/
Parent2/Child1
Parent1/Child1
Parent1/Child1/GrandChild1
Parent1/Child2

I would like to display it something like this:

+ Parent1
  - Child1
    - GrandChild1
  - Child2
+ Parent2
  - Child1

Any help would be appreciated, thanks!

This really has nothing to do with SQL.

  • Take an individual path, say foo/bar/baz/bat .

  • Split that string into an array of path segments: string[]segments = path.split('/') ; . This array represents the path of nodes in the tree you're going to construct.

  • Iterate over that list of segments to build out the tree structure desired — whether that be a treeview control or some other sort of tree structure — if the node at a given path in the tree doesn't exist, add it as you traverse the tree.

  • When you get to the leaf node, add the desired data related to that path.

Repeat the above procedure for each path in your list of paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM