Clear a TreeView

Beef picture Beef · Jun 22, 2011 · Viewed 59.9k times · Source

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.

Answer

rifaco picture rifaco · Mar 22, 2013

To clear a treeview, you clear the nodes.

treeView1.Nodes.Clear();