How to expand only the selected node in treeview control?
I have a treeview control. By default the treeview expands and shows all its child nodes. I need to show only the node that is selected.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Mar 2, 2015, 6:43 PM EST
Go through following articles it will help you
http://stackoverflow.com/questions/14596723/how-can-i-get-all-expanded-nodes-in-treeview
https://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.checkboxes%28v=vs.110%29.aspx
http://www.codeproject.com/Questions/196856/C-NET-Treeview-Node-Checked
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 2, 2015, 7:06 PM EST
see demo with source code.
http://www.dynamicdrive.com/dynamicindex17/ddaccordionmenu-bullet.htm
http://www.dynamicdrive.com/dynamicindex17/ddaccordionmenu.htm
Deepa Sudhir35 reputationPosted Mar 2, 2015, 10:27 PM EST
Deepa Sudhir35 reputationPosted Mar 2, 2015, 10:30 PM EST
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 2, 2015, 10:44 PM EST
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
if (e.Node.Parent == null)
return;
string strNodeValue = e.Node.Value;
foreach (TreeNode node in e.Node.Parent.ChildNodes)
{
if (node.Value != strNodeValue)
node.Collapse();
}
}
Deepa Sudhir35 reputationPosted Mar 2, 2015, 10:51 PM EST
It works fine only for the first node and its children.
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 2, 2015, 10:59 PM EST
{
if (e.Node.Parent == null)
return;
string strNodeValue = e.Node.Value;
foreach (TreeNode node in e.Node.Parent.ChildNodes)
{
if (node.Value != strNodeValue)
node.Collapse();
}
}
working fine for me
Deepa Sudhir35 reputationPosted Mar 2, 2015, 11:04 PM EST
Is there any other property to be set here?
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 2, 2015, 11:15 PM EST
but after bind treeview
try once
TreeViewArtMaterial.CollapseAll();
and below link may help you
http://www.dotnetcurry.com/showarticle.aspx?ID=257
Deepa Sudhir35 reputationPosted Mar 2, 2015, 11:31 PM EST
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 2, 2015, 11:34 PM EST
or visit link
http://www.dotnetcurry.com/showarticle.aspx?ID=257
Deepa Sudhir35 reputationPosted Mar 2, 2015, 11:42 PM EST
Khargesh RajputRank #616 · 3.9K reputationPosted Mar 3, 2015, 12:13 AM EST
http://www.dotnetsharepoint.com/2014/04/click-node-text-to-expandcollapse.html
http://forums.asp.net/t/1194490.aspx?Treeview+Navigation+Expand+and+Collapse+on+Node+click
Vishnu BhangaleRank #9,687 · 203 reputationPosted Mar 3, 2015, 12:20 AM EST