?
Solved

Tree View control ... adding a new node

Posted on 2004-10-07
20
Medium Priority
?
388 Views
Last Modified: 2010-04-15
After I add a new node to a tree.........how do I refresh the TreeView and SELECT the node I just added?
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 12256013
you would do something like

TreeView myTreeView;

TreeNode nodeToAdd = new TreeNode( "MyNodeToAdd" );
// Code adding the node
// This should be enough so as to change the selected node of the tree to the one just added.
myTreeView.SelectedNode = nodeToAdd ;        

Hope this helps

Tincho
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12256298
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12256304
This can have all the answers about tree view
http://www.syncfusion.com/FAQ/WinForms/FAQ_c91c.asp
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:Razzie_
ID: 12257301
Could've asked this in your previous question :)

TreeNode newNode = new TreeNode("NewNode");
treeView1.Nodes.Add(newNode);
treeView1.SelectedNode = newNode;
treeView1.Refresh();
treeView1.Focus();

If you'd want to add it to another node, you could use treeView1.TopNode.Nodes.Add(newNode); for example.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12261436
I should explain something I am doing.

I am storing the Tree View in memory...........then building the nodes seen in the GUI control at runtime from scratch....so I don't lose data.

I have an "IsSelected" field in the database table that represents the Tree nodes which tells me if the node should be selected after it is created in the GUI control.

But I am having a hard time getting the Selected state to STICK.....

I need to build the entire TreeView first.....then go back and select the node   where  "IsSelected" is true in the database table.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12263966
Well if you're not storing properties like the nodes' index in the treeview you might want to select the right node recursively like the method I gave before:

private void traverseNodes(TreeNode node)
{

    foreach(TreeNode childNode in node.Nodes)
    {
         if(childNode.Text == yourDataBaseObtainedTextPropertyForThatNode)
         {
             treeView1.SelectedNode = childNode;
             treeView1.Refresh();
             treeView1.Focus();
             return;
          }
         traverseNodes(childNode);
    }
}
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12263992
Maybe you can help me understand something.....

When you say NODE INDEX...........what are you referring to exactly?

It seems to me that the NODE INDEX is relative to the parent.    Is there an ABSOLUTE NODE INDEX that is unique for every node that I can use to navigate and select nodes with??????


My apologies for being such a dummy.  :)
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12264045
No, because you never know if index '3' means

0 ROOT
   1 Child1
   2 Child2
   3 Child3

Or

0 ROOT
   1Child1
   2Child2
      3SubChild1
      4SubChild2
   5Child3

I meant more like: are you saving the treenode structure to database (like parent and childid's as columns in the database, or something:) If not, the other method should work.

Off to sleep, later :)

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12264089

Here is the table I use to build the tree:

ID      Category                 ParentID      IsSelected
164      Inventory Items                   0                  No
171      Tom New Stuff                164            Yes


ID --------  is just an Access autonumber field (no correlation to the TreeView at all)
Category ----------- is just a string
ParentID   ---------  is the ID for the parent of the row....  0   if there is no parent
IsSelected  ---------   boolean.........my attempt to record if a node should be selected or not after the tree is built from the table.


Here is a screenshot of the resulting treeview  (built from the database table):

http://www.robotzgame.com/junk/treeviewss.bmp

0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12265493
How do you build your treeview? based on what data? not the database I take it, since you said you build it from scratch. But if not from database, why are you saving all nodes there? For future use? Or only to check what node should be selected?

If the 'Category' column is indeed the text on the treenodes this would work:

sql = "SELECT Category FROM yourtable WHERE IsSelected = 'Yes'";
// string sqlResult = get the result

private void traverseNodes(TreeNode node)
{

    foreach(TreeNode childNode in node.Nodes)
    {
         if(childNode.Text == sqlResult)
         {
             treeView1.SelectedNode = childNode;
             treeView1.Refresh();
             treeView1.Focus();
             return;
          }
         traverseNodes(childNode);
    }
}



0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12268623
I don't want to base it on child node TEXT in care there are duplicates, and for now, I want to allow dups.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12269978
Then there is not an easy way to tell which node to select. You can use the combination of parentnode and childnode to be unique, so one node's text could occur multiple times but not in combination with the same parent node. Just add an sql statement that selects the parentnode's text of the node with IsSelected = 'Yes' and you can use this. If that is not enough, you can even select THAT node's parent, until you reach the root parent in the database.
The code would look like this:

private void traverseNodes(TreeNode node)
{

    foreach(TreeNode childNode in node.Nodes)
    {
         if(childNode.Text == sqlResultNodeText && node.Text == sqlResultParentNodeText)
         {
             treeView1.SelectedNode = childNode;
             treeView1.Refresh();
             treeView1.Focus();
             return;
          }
         traverseNodes(childNode);
    }
}

If you really don't want to go this way, I'd have to repeat my question: why do you want to do it this way? do you build your treeview based on the database? Or is that only to check which node to select?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12277486
I build the entire TreeView from the contents of a database.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12277490
Here is the table I use to build the tree:

ID     Category                ParentID     IsSelected
164     Inventory Items                  0                 No
171     Tom New Stuff               164            Yes


ID --------  is just an Access autonumber field (no correlation to the TreeView at all)
Category ----------- is just a string
ParentID   ---------  is the ID for the parent of the row....  0   if there is no parent
IsSelected  ---------   boolean.........my attempt to record if a node should be selected or not after the tree is built from the table.


Here is a screenshot of the resulting treeview  (built from the database table):

http://www.robotzgame.com/junk/treeviewss.bmp
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12277814
Then when creating the nodes from the database you can keep a reference to the node with the IsSlected vale to Yes. When you're done building the treeview, you can use treeView.SelectedNode = yourReferencedNode; and you would be done.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12277847
Yes...this would work if name duplication were not possible.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12277858
Why is there no apparent way to uniquely identify each node (using a GUID or something) and store that in the database table?

0
 
LVL 8

Accepted Solution

by:
Razzie_ earned 2000 total points
ID: 12278211
wait, I'm not talking about duplicate names here. Just have a nodeToSelect local treenode in your class:

the 'code' would then be:

while(reading from database)
{
   TreeNode node = new TreeNode(database category value);
   //add node to treeview
   if(check if IsSelected == "Yes")
       this.nodeToSelect = node;
}

treeView.SelectedNode = this.nodeToSelect;
treeView.Refresh();
-----------------------------------------------------------------

That should work...

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12278271
I see what you are doing....yes.....that should work.

I'll come back here if it does not.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12278918
I could've waited with the accept :) Hope it work, though.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question