【问题标题】:How to add a new treeview at the selected node?如何在选定的节点上添加新的树视图?
【发布时间】:2011-02-09 17:44:03
【问题描述】:

我有一个四层的树视图;父母,孩子,孙子,曾孙。我的 selectednode 处于孙级别。

我想做的是在孙子创建一个新的“Treeview” - 不,我不想为“selectednode”(孙子)创建一个新节点。所以应该是这样的:

父母 孩子 孙子(新 TreeView)父子,是孙子
曾孙子
曾孙 孙子孙子

这将类似于父表,其中母亲和父亲离开并与现有孩子的配偶不同的配偶生了新孩子。

   Private Sub PopulateRootLevel()
            ' query to find first round of parent
            PopulateNodes(dt, JCATreeView.Nodes)
    End Sub

Private Sub PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection)
        For Each dr As DataRow In dt.Rows
            Dim tn As New TreeNode()
            tn.Text = dr("TITLE").ToString()
            tn.Value = dr("Parent_ID").ToString()
            nodes.Add(tn)

            'If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
        Next
End Sub

Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal parentNode As TreeNode)

        ' query to find children of parent with child node count of parent
         da.Fill(dt)
        PopulateNodes(dt, parentNode.ChildNodes)
End Sub

Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, _
  ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
        ' add a test to determine if this is from TreeView1 or Sub_TreeView1
         PopulateSubLevel(CInt(e.Node.Value), e.Node)
End Sub

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged

        Dim selected_parent_id As Integer = sender.SelectedNode.value
        Parent_to_NEW_TREEVIEW_PopulateSubLevel(selected_parent_id, sender.SelectedNode)
End Sub

Private Sub Sub_TreeView1_PopulateSubLevel(ByVal parent_id As Integer, ByVal parentNode As TreeNode)

        ' Query to get new children of parents
        da.Fill(dt2)
        Sub_TreeView1_PopulateNodes(dt2, parentNode.ChildNodes)
End Sub

    Private Sub Sub_TreeView1_PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection)
        For Each dr As DataRow In dt.Rows
            Dim tn As TreeNode = New TreeNode()
            'tn = parentBCNode.Nodes.Add("NEW_PARENT_TREEVIEW")

            ' query to get child on the new parent treeview

            tn.Text = dr("New parent title").ToString()
            tn.Value = dr("New_parent_ID").ToString()
            nodes.Add(tn)

            'If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
        Next
    End Sub

【问题讨论】:

  • 一个新的“树视图”只是另一个节点。真正的问题是应该在哪里添加新节点?它是所选节点的兄弟节点吗?

标签: vb.net


【解决方案1】:

你不能那样做。 TreeView 不能将另一个 TreeView 控件作为其子节点之一。您唯一能做的就是将 TreeView 分配给 TreeNode 的 tag 属性,但这不会显示(显然)。 我不明白您为什么要这样做,除非您想要为该孙子树使用不同的绘图行为。 您可以使用 treeNode.Level 属性来找出该节点所在的级别。再次,您可以创建一个自定义对象(包含您所需的所有信息)并将其存储在 treeNode.Tag 属性中。

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 2012-12-21
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多