Negative parent ID on nodes

8 posts, 0 answers
  1. Sonya L
    Sonya L avatar
    36 posts
    Member since:
    Dec 2009

    Posted 17 May 2011 Link to this post

    I have a treeview bound to a binding list, and I have noticed that when I add items to the binding list with a negative number (other than -1) as the parent ID, the treeview control clears out and shows just that node.  Does anyone know why this might be happening?  Thanks!
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 20 May 2011 Link to this post

    Hello Sonya L,

    Thank you for contacting us.

    In this case you should use numeric properties for ID and ParentID. When using a DataTable, you can change the column type. Please consider the following example:

    DataTable table = new DataTable();
    table.Columns.Add("Id", typeof(int));
    table.Columns.Add("ParentId", typeof(int));
    table.Columns.Add("Name");
     
    table.Rows.Add(0, -1, "Root1");
    table.Rows.Add(1, -1, "Root2");
    table.Rows.Add(2, -1, "Root3");
     
    table.Rows.Add(3, 0, "Child1");
    table.Rows.Add(4, 1, "Child2");
    table.Rows.Add(5, 2, "Child3");
     
    radTreeView1.DataSource = table;
    radTreeView1.ParentMember = "ParentId";
    radTreeView1.ChildMember = "Id";
    radTreeView1.DisplayMember = "Name";

    I hope it helps. Should you have any further questions, do not hesitate to ask.

    Kind regards,
    Julian Benkov
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
  3. Sonya L
    Sonya L avatar
    36 posts
    Member since:
    Dec 2009

    Posted 24 May 2011 Link to this post

    I was looking for a solution that allowed the ParentID to be a negative number other than -1, so that if I had a node whose ID was -101, it could have children whose parent ID was -101.  Since this doesn't work in bound mode (a lot of other things don't work in bound mode either), I had to rewrite my code to build the tree without binding it to data.  If the tree is not bound, this no longer is a problem.
  4. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 27 May 2011 Link to this post

    Hello Sonya L,

    I logged the issue in our Issue Tracking System. The fix will be available in the next official release.
    Currently, to work properly in this scenario, the ParentID number for all root Nodes must have the same number. For example, this can be a '-101' or '-1'. 

    Greetings,
    Julian Benkov
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
  5. Phil
    Phil avatar
    26 posts
    Member since:
    Oct 2012

    Posted 24 Jul Link to this post

    I have encountered this problem with BindingList as the DataSource for a RadTreeView in the latest 2017 R2 SP1 release.

    If you create a new child node with a negative ID (ChildMember), say -1000, and then create a child of that node, also with a negative ID, say -1001, then all previous nodes get wiped out and only the most recently created node is displayed.

    This is specifically an issue when creating a child node of a node with a negative ID. Using postive IDs does not trigger this issue.

    Create a new project with a single RadTreeView and a single button and paste this code. Now select any node and click the button twice. After the first click the new node is correctly added and then selected. After the second click the next node is added but all previous nodes disappear from view.

    Public Class Thing
     
        Public Property ID As Integer
     
        Public Property Name As String
     
        Public Property ParentId As Integer
     
    End Class
     
    Private Things As New BindingList(Of Thing)
     
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     
        Dim Thing1 As New Thing()
        Thing1.ID = 1
        Thing1.Name = "Level 1"
        Thing1.ParentId = 0
     
        Dim Thing2 As New Thing()
        Thing2.ID = 2
        Thing2.Name = "Level 2"
        Thing2.ParentId = 1
     
        Dim Thing3 As New Thing()
        Thing3.ID = 3
        Thing3.Name = "Level 3"
        Thing3.ParentId = 2
     
        Things.Add(Thing1)
        Things.Add(Thing2)
        Things.Add(Thing3)
     
     
        RadTreeView1.DataSource = Things
        RadTreeView1.ParentMember = "ParentId"
        RadTreeView1.ChildMember = "Id"
        RadTreeView1.DisplayMember = "Name"
     
    End Sub
     
    Private ID As Integer = 0
     
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     
        ID -= 1
     
        Dim Thing As New Thing()
        Thing.ID = ID
        Thing.Name = "Level " & Thing.ID
        Thing.ParentId = DirectCast(Me.RadTreeView1.SelectedNode.DataBoundItem, Thing).ID
     
        Things.Add(Thing)
        Me.RadTreeView1.FindNodes(Thing.Name).First().Selected = True
     
    End Sub
  6. Dess
    Admin
    Dess avatar
    1995 posts

    Posted 26 Jul Link to this post

    Hello Phil, 

    Thank you for writing.  

    The current implementation of RadTreeView's data layer uses sorting the nodes to determine root nodes and their children. However, negative numbers break that sorting. We had a similar issue logged in our feedback portal and we decided to decline such an item. Here is the feedback item for your reference: https://feedback.telerik.com/Project/154/Feedback/Details/109601-fix-radtreeview-negative-parent-id-on-nodes

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  7. Phil
    Phil avatar
    26 posts
    Member since:
    Oct 2012

    Posted 26 Jul Link to this post

    Thanks for the background info, it explains why the tree behaves that way.

    I've worked around the issue by setting all new objects to have absurdly large IDs that should never be encountered during normal use.

    I disagree with sorting being a legitimate reason for excluding negative ID's though especially since the root nodes will always be the nodes without parents, irrespective of whether the parent ID is -1 or 0.

    Negative IDs are fairly common, especially when adding new temporary objects that aren't saved to disk/database yet.

    A requirement for a root level node to have a parent of 0 is fair enough - a null would actually be better since 0 can also be a legitimate ID whereas null should never be.

    Besides this issue the tree works well. In a worst case scenario I can see the load on demand being feasible but the tree is very easy to use with basic binding whereas the load on demand adds a lot of extra code.

  8. Dess
    Admin
    Dess avatar
    1995 posts

    Posted 27 Jul Link to this post

    Hello Phil, 

    Thank you for writing back. 
     
    Your feedback is greatly appreciated. We will consider it in the future control's improvement if it is possible to improve the data layer to support negative ids.  
    As it is suggested in the feedback item, you can use the load on demand approach for populating RadTreeView with data: http://docs.telerik.com/devtools/winforms/treeview/data-binding/load-on-demand

    If you have any additional questions, please let me know. 

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top