7 Answers, 1 is accepted
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
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'.
Julian Benkov
the Telerik team
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
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
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.
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