refreshing a treeview

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Hi,
I've got a treeview on the left of my form, filled with data from a
database, and the user can enter new data on the right side of the
form. When the user saves the data, I'd like the tree to be refreshed.
For now I just rebuild the tree entirely, which works, but it is
collapsed and I don't know how to expand it to the node that has just
been created. I'm struggling with that issue, so if someone has ever
done that before, please help me !
thx
 
Sam said:
Hi,
I've got a treeview on the left of my form, filled with data from a
database, and the user can enter new data on the right side of the
form. When the user saves the data, I'd like the tree to be refreshed.
For now I just rebuild the tree entirely, which works, but it is
collapsed and I don't know how to expand it to the node that has just
been created. I'm struggling with that issue, so if someone has ever
done that before, please help me !
From the docs:
TreeNode.EnsureVisible Method

Ensures that the tree node is visible, expanding tree nodes and
scrolling the tree view control as necessary.

When the EnsureVisible method is called, the tree is expanded and
scrolled to ensure that the current tree node is visible in the
TreeView. This method is useful if you are selecting a tree node in
code based on certain criteria. By calling this method after selecting
the node, it ensures that the user can see and interact with the
selected node.
Easy if you know it :)
 
I'm sorry but I really don't see how EnsureVisible is related to my
problem? I'm having troubles with expanding the tree to the newly added
node, not with displaying my treeview.
Can you help further ?
thx
 
Sam said:
I'm sorry but I really don't see how EnsureVisible is related to my
problem? I'm having troubles with expanding the tree to the newly added
node, not with displaying my treeview.
Can you help further ?

Um...

When the EnsureVisible method is called, the tree is expanded and
scrolled to ensure that the current tree node is visible in the
TreeView.

Isn't that what you just said you want?
 
I agree. You should be able to determine which node was just
added/edited. After you updated the database and re-display the
treeview, set the "new" node to EnsureVisible=True.

Thanks Larry for this. I had the same problem with an old project, and
just couldn't figure it out.

John
 
You should be able to determine which node was just
added/edited

Even if I don't use EnsureVisible, my newly added node is in the tree
and if I expand the tree manually, I can see it. What I don't know is
how to expand the tree to that node !
Do you see what I mean ?
 
Sam said:
You should be able to determine which node was just
added/edited

We can't tell you that. Presumably, when the user enters new data and
commits that change, you save data to an underlying data store? Then
you say you rebuild the tree from that data store - well, while you are
doing this, it should be possible to know when you are creating the
node that corresponds to the data the user just entered. Keep a
reference to that node, then when you have finished rebuilding the
tree, you only need do

newNode.EnsureVisible

Are you sure you need to completely rebuild the tree every time data is
entered?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top