开发者

ASP.NET Treeview From Path

开发者 https://www.devze.com 2023-03-07 00:23 出处:网络
I have a data hierarchy path separated by \'/\', currently in a SQL Server 2008 table. How can I display this path as开发者_Python百科 a TreeView in my ASP.NET C# application.

I have a data hierarchy path separated by '/', currently in a SQL Server 2008 table. How can I display this path as开发者_Python百科 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消