开发者

C# TreeView Nodes

开发者 https://www.devze.com 2023-02-20 10:02 出处:网络
I\'m going to make this as clear as I can, if I leave any details out that would help you help me get it working right, please let me know.

I'm going to make this as clear as I can, if I leave any details out that would help you help me get it working right, please let me know.

Ok, so what I want to do is:

I have a treeview list, which I want to add Parent Nodes and Child Nodes to, based on ID's.

So, ID's could come in like 32736 and I want it added to the Treeview (Even if it doesn't have any value between 6 - 32736)

Is th开发者_JAVA技巧ere anyway possible of doing this? thanks.


You can add nodes with any key (id) you want if you're adding them manually.

See here: http://msdn.microsoft.com/en-us/library/57aa8e09.aspx for WinForms. It's pretty much the same code for the Asp.Net TreeView.

code for defining a new Asp.Net Treenode here: http://msdn.microsoft.com/en-us/library/12bxet86.aspx

Then just add it to the collection

TreeNode myNewNode = new TreeNode("SomeTextToDisplay", "SomeId");
myTreeView.Nodes.Add(myNewNode);


Check out http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.nodes.aspx When adding the nodes make sure to do it from the primary UI thread to avoid a cross thread exception or use the TreeView.BeginInvoke() method to have the action performed on the correct thread.

TreeViewToModify.BeginInvoke(delegate() => TreeViewToModify.Nodes.Add(new TreeViewNode(id)));

Keep in mind this is not taking into account adding to specific parent/child nodes but the logic is the same.


TreeView1.Nodes.Clear();
TreeNode root = new TreeNode("Base");
TreeView1.Nodes.Add(root);
TreeNode sub = new TreeNode("32736");
root.Nodes.Add(sub);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号