Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I hope I can explain this so it makes sense.

I've got a TreeView that I put a list of items, sub-items, and sub-sub-items into. These are Timers, TagGroups, and Tags respectively.

Right now, when anything new is added, I reload my data structures from my DB and then clear and reload the TreeView. The result is that everything gets closed and I have to re-expand anything I had open.

This isn't horrible in context but it obviously isn't particularly nice if the user wants to add 6 or 8 tags to the same Tag Group. (In truth they can do that without going back to the Tag Group ... but human nature suggests they're going to anyway)

What I would like is to remain on the item that was last added or edited; even though I've cleared the Tree View and recreated it.

The contents of the Tree View are sorted when pulled from the DB so the position of a given element isn't guaranteed to remain in the same place ... although I will always know what it's called.

So ... any recommendations about how to best handle this so the last added/edited item remains visible and selected? Code examples would also be appreciated.

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Oct-11 19:19pm    
Good question, my 5.
One possibility: can you change database schema for this purpose?
--SA
Bala Selvanayagam 13-Oct-11 4:22am    
Good question

can you please post me the DB structure please
BHort 13-Oct-11 11:00am    
Unfortunately the DB structure wont help because the TV changes depending on UI selections which determine which features and elements to display.

That said, the DB structure is roughly as follows. It has 4 different tables. Timers -> Timers/Tag Groups XRef -> Tag Groups -> Tags

Tag Groups can show up under multiple timers, hence the XRef.

Too bad I don't know your database schema, but there is one more, better possibility: create "smart population" of the tree view. If you Modify the process of the population of the data after you feel new DataTables. Do what you did before, only do not re-create the tree and tree nodes, reconcile required data with the data already in the tree view, from the root down to all the child nodes.

—SA
 
Share this answer
 
This is my c# code, works for me for multi levels and hope you can convert into VB.NET.

Say for instance, i have a table called "products", data columns "id" (primaty & unique?) & "name"...blah blah... and set the "Key" element of the node to products->id when loading the tree view.

Now I am able to programatically select a node as follows for a given products->ID

treeView1.SelectedNode =  treeView1.Nodes.Find(txtFind.Text, true).First() ;


When you do the update, hope you know the products->id value and can find and highlight the node programatically after the update statement and loading the tree view again.

Similarly when you do an insert, you can get the inserted ID and do the same. Hope this helps but depends on how you have implemented the tree view in your system.


Let me know whether it helped
 
Share this answer
 
v2
Comments
BHort 13-Oct-11 10:58am    
Ok, I'm slowly figuring this out ... I think! ;-)

When I try and do a find, it's only looking in my 5 root nodes, not the sub-nodes. How do I get Find to also look in the sub-nodes?

Right now I'm getting a "Sequence contains no elements" error when I search for a sub-node that I know is there using text that is identical to the displayed text of the sub-node. (Actually it's a sub-sub-node but still)
BHort 13-Oct-11 11:03am    
By the way ... the syntax for VB.Net is virtually identical so your code snippet was helpful.
When I tried this solution:

If NameToFind > "" Then
  For Each RootNode As TreeNode In TreeView.Nodes
    If RootNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = RootNode
    For Each ChildNode As TreeNode In RootNode.Nodes
      If ChildNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = ChildNode
      For Each GrandChildNode As TreeNode In ChildNode.Nodes
        If GrandChildNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = GrandChildNode
      Next
    Next
  Next
End If


It did almost everything I wanted. The *only* problem was that it didn't leave the node highlighted. It selected the node but didn't display a highlight within the actual TreeView component.

Any suggestions?
 
Share this answer
 
Comments
BHort 13-Oct-11 11:57am    
The final piece of the puzzle was two minor adjustments.

First, turn HideSelection to false on the TreeView. Simple but important.

Second, give the TreeView focus.

Doing those two things gives the nice blue background on the selected item just as if the user had clicked on it.
Bala Selvanayagam 13-Oct-11 12:09pm    
Great work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900