TreeView

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there an easy way to check the checkboxes via Code based on previously
saved selection onto a database?

Thanks,

Victor
 
Hi Victor,

Thank you for your post.

From your message, I understand that you want to:
1) Save the selection of the TreeView to database
2) Load the selection of the TreeView from database later

I think how you achieve this largely depends on how you can uniquely
identify each node in the TreeView. If each node has unique Value, then you
can use TreeNode.ValuePath property as the key to save selected nodes. If
the nodes may have identical Value but the nodes count and order will not
change, then you can use the index of each node in its parent's ChildNodes
collection as the key to save selected nodes.

Either way, you need to recursively walk the TreeView's Nodes collection to
get/set the selection:

SaveTree(TreeView1.Nodes);

private void SaveTree(TreeNodeCollection tnc)
{
foreach(TreeNode node in tnc)
{
if (node.Checked)
{
...
}
SaveTree(node.ChildNodes);
}
}

Hope this helps. Please feel free to post here if anything is unclear.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you the ValuePath property help on the saving but I don't know if I
can find nodes directly with the ValuePath property instead of going thru
all the nodes and childs until I find the ones I need.

Please advice,

Victor
 
Hi Victor,

Thank you for your update.

You don't have to walk the tree to find the node with specified ValuePath,
you can use TreeView.FindNode instead:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.
findnode.aspx

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Great, Thank you very much, that solves my problem, now I'll be taking
advantage of the Treeview control fully.

Victor
 
Hi Victor,

Appreciate your update and response. I am glad to hear that the problem has
been fixed. If you have any other questions or concerns, please do not
hesitate to contact us. It is always our pleasure to be of assistance.

Have a nice day!

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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