Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

Tree View control ... adding a new node

After I add a new node to a tree.........how do I refresh the TreeView and SELECT the node I just added?
0
Tom Knowlton
Asked:
Tom Knowlton
  • 9
  • 8
  • 2
  • +1
1 Solution
 
tinchosCommented:
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
 
armoghanCommented:
This can have all the answers about tree view
http://www.syncfusion.com/FAQ/WinForms/FAQ_c91c.asp
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Razzie_Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
Razzie_Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
Razzie_Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:

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
 
Razzie_Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
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
 
Razzie_Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
I build the entire TreeView from the contents of a database.
0
 
Tom KnowltonWeb developerAuthor Commented:
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
 
Razzie_Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
Yes...this would work if name duplication were not possible.
0
 
Tom KnowltonWeb developerAuthor Commented:
Why is there no apparent way to uniquely identify each node (using a GUID or something) and store that in the database table?

0
 
Razzie_Commented:
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
 
Tom KnowltonWeb developerAuthor Commented:
I see what you are doing....yes.....that should work.

I'll come back here if it does not.
0
 
Razzie_Commented:
I could've waited with the accept :) Hope it work, though.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 8
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now