20

I'm loading a TreeView from a list, and the user has a button to delete an item and it deletes it from the list no problem, but there is also a button to update the TreeView with the list after items have been deleted, I have no problem adding the new items to the TreeView but is there a way to clear all the items in the TreeView before I add new items, so I don't have duplicates in the TreeView? I have tried looking on other spots on the internet for the answer but cant find it, I've tried simple things like:

treeView1.Items.Clear();

but it doesn't work.

Sorry, I mentioned it on a comment, below that I'm pretty sure this line does in fact clear it, I am just not using it in the right place, thanks all for your answers.

5
  • 5
    Have you tried treeView1.Nodes.Clear()? Jun 22, 2011 at 20:52
  • 1
    looked into it, it doesnt let you do the .Nodes
    – Beef
    Jun 22, 2011 at 21:04
  • 1
    Are you sure your treeView1 is a TreeView? I have no option for .Items, only .Nodes. Jun 22, 2011 at 21:13
  • 1
    @Kyle: .Items is right. Are you sure you're using WPF? Jun 22, 2011 at 21:15
  • Damn, didn't even see the wpf in there. My bad, I'm an idiot :/ Jun 22, 2011 at 21:17

4 Answers 4

46

To clear a treeview, you clear the nodes.

treeView1.Nodes.Clear();
1
  • 2
    NOTE: This answer works for Windows.Forms.TreeView. (The original question is tagged WPF, so it is about Windows.Controls.TreeView) Nov 29, 2018 at 15:48
4

First of all, Items and Clear should be capitalized in your example. Maybe that's the only problem.

Second, if you are populating the tree by setting its ItemsSource, then you are not allowed to add and remove items from its Items collection by hand. Instead, you should make the source an ObservableCollection instead of a List. Then the treeview will automatically update itself to reflect changes in the source collection

3
  • those are capitalized in the program, just forgot to in the question, for populating the the tree, i make a new object TreeViewItem such as rootDir and add the items to it, then add to the treeView like so: treeView1.Items.Add(rootDir);
    – Beef
    Jun 22, 2011 at 21:01
  • @Beef have you tried writing the smallest test program possible to demonstrate this behavior? Sometimes seemingly unrelated complications can get in the way, especially when dealing with the fairly complex beasts that are GUI controls. Jun 22, 2011 at 21:04
  • Ive been playing around with the treeView1.Items.Clear(); line and i think it is clearing but i must be adding to the treeview somewhere else as well, if i comment the line out it adds to treeView twice, so maybe i could have it in the wrong spot
    – Beef
    Jun 22, 2011 at 21:11
0

did you try

treeView1.DataBind();
1
  • 1
    I havnt used that before, is there a library i need for it? cause intellisense isnt recognizing it
    – Beef
    Jun 22, 2011 at 20:55
0

I had a recursive method to populate the TreeView after clearing it, when doing a refresh. I was passing the TreeNode by reference and it would cause the nodes to remain visible and selectable in the TreeView even though the Clear logically removed them from the Nodes list. When I changed the method to pass the TreeNode by value it correctly allowed me to clear the nodes. The ByRef in the sample below had to be changed to ByVal (for VB.NET).

Private Sub DisplayPackageSubTree()
  trvEntries.Nodes.Clear()
  trvEntries.SelectedNode = trvEntries.Nodes.Add("Node0", "Root Node", -1, -1)

  DisplayFolderTree(_folderContents, trvEntries.Nodes(0))

  trvEntries.ExpandAll()
  trvEntries.SelectedNode = trvEntries.Nodes(0)
  trvEntries.Refresh()
End Sub

Private Sub DisplayFolderTree(ByVal folderContents As SubFolder, ByRef folderNode As TreeNode)
  For Each entry As SubEntry In folderContents.Contents
    If TypeOf entry Is SubFolder Then
      Dim newNode As TreeNode = folderNode.Nodes.Add(entry.Name, entry.Name, 0, 0)
      DisplayFolderTree(entry, newNode)
    ElseIf TypeOf entry Is SubDocument Then
      folderNode.Nodes.Add(entry.Name, entry.Name, 1, 1)
    End If
  Next
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.