Solved

Tree View control ... adding a new node

Posted on 2004-10-07
20
379 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 124
Reference issues in Visual Studio 3 41
C#.net split string and then check appropriate checkboxlist 4 26
Change to file doesn't show up 16 50
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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