rulec444

rulec444

  • NA
  • 81
  • 0

TreeView find and remove child nodes

Jul 7 2004 4:20 PM
I have a treeview that I add nodes to it from a call to a database. If the user modifies the items they are selecting in a listbox, then I want to "refresh" the treeview with their newly selected items. I have attempted code that will delete the items but I keep receiving a "Object reference not set to an instance of an object." When I step through the code I see that it is deleting a node, then it looks for the same node again. Here is my code - also, more information about building the treeview is under this thread: http://216.26.160.137/Forums/AspNetForums20/ShowPost.aspx?PostID=7572 In button save click event I have: TreeNode nodeD = FindNodeDelete(tvFilters, "tnState"); TreeNode nodeDChild = new TreeNode(); nodeD.Nodes.Remove(nodeDChild); private TreeNode FindNodeDelete(TreeView tvFitlers, string strText) { foreach (TreeNode nodeD in this.tvFilters.Nodes) { if(nodeD.Tag.ToString() == strText) { TreeNode nodeChildD = FindChildNodeDelete (nodeD, strText); if(nodeChildD !=null) { return nodeChildD; } } } return (TreeNode)null; } private TreeNode FindChildNodeDelete(TreeNode nodeStartD, string strText) { foreach (TreeNode nodeD in nodeStartD.Nodes) { if(nodeD != null) { nodeStartD.Nodes.Remove(nodeD); } else { TreeNode nodeChildD = FindChildNodeDelete(nodeD, strText); if(nodeChildD != null) return nodeChildD; } } return (TreeNode)null; } The nodes under State are "Equal", "NY", "Equal", "NH". It finds "Equal" and deletes it, finds "Equal" and deletes it, then finds "NH", deletes it, then for some reason, it finds "NH" again and trys to delete it but its already been deleted. Which is when I receive the error. I'm not sure why its reading each node twice....and its not even reading them in the order I put them in.