Generate list of parents-childs (recursive relationship) from Dataset

淺唱寂寞╮ 提交于 2019-12-11 09:29:12

问题


I'm trying to build a list (HTML) with a recursive relationship. The data is in a dataset but could converted to a data table if it's easier.

I don't know what's the best option to achieve this. I was thinking about using nested repeaters.

Here's the data:

__ID__ | __NAME__  | __PARENT__     | __LEVEL__ 
1      | Patrick   |                | 1           
2      | Mark      |                | 1
3      | Scott     | 2              | 2
4      | Jason     |                | 1
5      | Julian    |                | 1
6      | John      | 6              | 2
7      | Steve     |                | 1
8      | George    | 1              | 2
9      | Robert    | 1              | 2 
10     | Rodney    | 8              | 3

Here the output I want to produce

- Patrick [1]
  - George [8]
    - Rodney [10]
  - Robert [9]

- Mark [2]
  - Scott [3]

- Julian [5]
  - John [6]

- Jason [4]

- Steve [7]

回答1:


The easiest way to do this is to write a recursive method. The way it operates will depend on whether you want to have the method return the entire tree-structured list, or output the data as it reads it. If you want to output the data as you read it, your code might look something like this:

Private Sub OutputTree(data As DataTable, parentId As String, indentationLevel As Integer)
    For Each row As DataRow In GetChildRows(parentId)
        OutputRow(row, indentationLevel)
        OutputTree(data, row("ID").ToString(), indentationLevel + 1)
    Next
End Sub

The above code assumes that you also implement a method called GetChildRows which returns a list of all the rows that contain the given parent ID. It also assumes that you have a method called OutputRow which outputs the given row at the given indentation level.

Then, you could call the method like this:

OutputTree(myDataTable, nothing, 0)

If you want to build and return a tree structure, which is arguably the better approach, your code might look something like this:

Private Function BuildTreeNodes(data As DataTable, parentId As String) As List(Of MyTreeNode)
    Dim nodes As List(OfNew MyTreeNode)()
    For Each row As DataRow In GetChildRows(parentId)
        Dim node As New TreeNode()
        node.Row = row
        node.Children = BuildTreeNodes(data, row("ID").ToString())
        nodes.Add(node)
    Next
    Return node
End Sub

The above code assumes that you have defined a MyTreeNode class which would look something like this:

Public Class MyTreeNode
    Public Property Row As DataRow
    Public Property Children As List(Of MyTreeNode)
End Class

Then you could call the method like this:

Dim rootLevelNodes As List(Of MyTreeNode) = BuildTreeNodes(myDataTable, Nothing)



回答2:


This will work if your __PARENT__ of parent item is null.

private void TopItems(DataTable Data)
{
    DataView view = new DataView(Data);
    view.RowFilter = "itemParent IS NULL";

    foreach (DataRowView row in view)
    {
        response.write("parent text: " + row["text"].ToString() + 
                 "parent id: " + row["id"].ToString();
        AddChildMenuItems(Data);
    }
}


//This code is used to recursively add child items by filtering by ParentID
private void AddChildtems(DataTable Data)
{
    DataView view = new DataView(Data);
    view.RowFilter = "itemParent=" + parentMenuItem.Value;
    foreach (DataRowView row in view)
    {
        response.write("child text: " + row["text"].ToString() + 
                 "child id: " + row["id"].ToString();
        AddChildtems(Data);
    }
}


来源:https://stackoverflow.com/questions/17858701/generate-list-of-parents-childs-recursive-relationship-from-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!