[英]ASP.NET Treeview From Path
I have a data hierarchy path separated by '/', currently in a SQL Server 2008 table. 我有一个用'/'分隔的数据层次结构路径,当前在SQL Server 2008表中。 How can I display this path as a TreeView in my ASP.NET C# application.
如何在ASP.NET C#应用程序中将此路径显示为TreeView。
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. 这确实与SQL无关。
Take an individual path, say foo/bar/baz/bat
. 采取个人路径,例如
foo/bar/baz/bat
。
Split that string into an array of path segments: string[]segments = path.split('/') ;
将该字符串分成路径段数组:
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. 对路径列表中的每个路径重复上述过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.