Solved

Tree View control ... adding a new node

Posted on 2004-10-07
20
376 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:knowlton
  • 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
 
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: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: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: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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by: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:knowlton
ID: 12277486
I build the entire TreeView from the contents of a database.
0
 
LVL 5

Author Comment

by: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:knowlton
ID: 12277847
Yes...this would work if name duplication were not possible.
0
 
LVL 5

Author Comment

by: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 500 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: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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now