Clearing Treeview Contents?

Troy

Well-known member
Joined
Feb 7, 2005
Messages
153
Programming Experience
10+
ok I have a problem with my Treeview in .net 2005. After I've populated my treeview control with all of my nodes I need a way to wipe the treeview clean.

and suggestions?

I've already Tried

TreeView1.Nodes.Clear()

It says this removes all treenodes from collection. But it doesn't remove them. when I repopulate the tree it shows double and tripple the information
 
Last edited:
TreeView1.Nodes.Clear() does remove all the nodes from the treeview, so there must be some other problem with your code.
 
Here's my code to populate my Treeview.
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PopulateTreeview()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] indx [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Short[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] currentAlpha [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sContactName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2]indx = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2]currentAlpha = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2]sContactName = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2]rowCustomer = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Populate Treeview Control[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] indx = Asc([/SIZE][SIZE=2][COLOR=#800000]"A"[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] Asc([/SIZE][SIZE=2][COLOR=#800000]"Z"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]currentAlpha = Chr(indx)[/SIZE]
[SIZE=2]TreeView1.Nodes.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] TreeNode(currentAlpha))[/SIZE]
[SIZE=2][COLOR=#008000]' Add a child TreeNode for each Author in the matching Main Node. [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] rowCustomer [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] dt.Rows[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (UCase(Mid(rowCustomer.Item([/SIZE][SIZE=2][COLOR=#800000]"LastName1"[/COLOR][/SIZE][SIZE=2]), 1, 1)) = currentAlpha) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]sContactName = rowCustomer.Item([/SIZE][SIZE=2][COLOR=#800000]"LastName1"[/COLOR][/SIZE][SIZE=2]) & [/SIZE][SIZE=2][COLOR=#800000]", "[/COLOR][/SIZE][SIZE=2] & rowCustomer.Item([/SIZE][SIZE=2][COLOR=#800000]"FirstName1"[/COLOR][/SIZE][SIZE=2]) & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & rowCustomer.Item([/SIZE][SIZE=2][COLOR=#800000]"MiddleInitial1"[/COLOR][/SIZE][SIZE=2]) & [/SIZE][SIZE=2][COLOR=#800000]"."[/COLOR][/SIZE]
[SIZE=2]TreeView1.Nodes(indx - 65).Nodes.Add(sContactName).Tag = rowCustomer.Item([/SIZE][SIZE=2][COLOR=#800000]"ContactID"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]

Here's my code to clear the Treeview.
VB.NET:
Expand Collapse Copy
[SIZE=2][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] mnuFileClose_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] mnuFileClose.Click[/SIZE]
 
[SIZE=2][COLOR=#008000]'Clear Treeview Nodes[/COLOR][/SIZE]
[SIZE=2]TreeView1.Nodes.Clear()[/SIZE]
[SIZE=2]cn.Close()[/SIZE]
 
[SIZE=2][COLOR=#008000]'ClearFields[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] ClearFields()[/SIZE]
 
 
'Update File Menu
[SIZE=2]mnuFileClose.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditAdd.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuFileSave.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuFileSaveAs.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuFilePrintPreview.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuFilePrint.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]'update Edit Menu[/COLOR][/SIZE]
[SIZE=2]mnuEditUndo.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditRedo.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditCut.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditCopy.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditPaste.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditDelete.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditEditRecord.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]mnuEditFindRecord.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]'update Toolbar[/COLOR][/SIZE]
[SIZE=2]AddRecordButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]EditRecordButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]FindRecordButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PrintPreviewButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PrintButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]UndoButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]RedoButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]CutButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]CopyButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PasteButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]DeleteRecordButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]GeneralSalesButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]EmployeeSalesButton.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/SIZE]
The problem is when I reopen the Database and attempt to repopulate my treeview It's like they were never empty and creates duplicate entries in the nodes. It will even create 3, 4 or more if I keep closing the file and opening it.


 
Last edited by a moderator:
Check DT each time.... or rather the Rows.Count of DT.... see if it's incrementing.... then look at how you are filling it.... I had a problem early on where I was using the .Fill method....with out realizing that .Fill does NOT first clear the Dt/DS before filling it....

-tg
 
Check DT each time.... or rather the Rows.Count of DT.... see if it's incrementing.... then look at how you are filling it.... I had a problem early on where I was using the .Fill method....with out realizing that .Fill does NOT first clear the Dt/DS before filling it....

-tg


ok Here's my open
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OpenDatabase()[/SIZE]
 
[SIZE=2]cn.ConnectionString = strConnection[/SIZE]
[SIZE=2]cn.Open()[/SIZE]
 
[SIZE=2][COLOR=#008000]' Fill the DataTable[/COLOR][/SIZE]
[SIZE=2]da.Fill(ds, [/SIZE][SIZE=2][COLOR=#800000]"Contact"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]dt = ds.Tables.Item([/SIZE][SIZE=2][COLOR=#800000]"Contact"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

I am using the .fill. But I don't know how to clear the data before. Is it .dispose?
 
Last edited by a moderator:
Nevermind I solved my problem I added Ds.clear to my close statement and that fixed it. The problem was in the .fill of my Table. Thanks TechGnome!
 
Back
Top