C # recursive Treeview and obtain the entire Node path based on the selected node

Source: Internet
Author: User

Private void loadtree ()
{
Gtree. nodes. Clear ();
DS = workdo. getdataset ();
Inittree (gtree. nodes, "0 ");
}
Private void inittree (treenodecollection NDS, string parentid)
{
Dataview DV = new dataview ();
Treenode tmpnd = NULL;
DV. Table = Ds. Tables [0];
DV. rowfilter = string. Format ("parentid = '{0}'", parentid );
Foreach (datarowview DRV in DV)
{
Tmpnd = new treenode ();
Tmpnd. Text = (string) DRV ["workname"]; // node name
Tmpnd. value = (string) DRV ["ID"];
// Tmpnd. navigateurl = string. Format ("? Id = {0} ", DRV [" ID "]); // node URL
// Tmpnd. imageurl = ""; // node Image
If (parentid = "0 ")
Tmpnd. Expanded = true;
Else
Tmpnd. Expanded = false;
NDS. Add (tmpnd );
Inittree (NDS [NDS. Count-1]. childnodes, DRV ["ID"]. tostring ());
}
}

 

/// <Summary>
/// When the metric point changes, the operation is displayed on the graphic interface.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Public event eventhandler selectednodechanged;
Protected void gtree_selectednodechanged (Object sender, eventargs E)
{
This. nodeid = gtree. selectednode. value;
// = Tree1.selectednode. valuepath; // complete path of Value

String STR = "";

Treenode parentnode = gtree. selectednode. parent;
While (parentnode! = NULL)
{
STR = "/" + parentnode. Text + STR;
Parentnode = parentnode. parent;
}
This. nodename = (STR + "/" + gtree. selectednode. Text). substring (1); // complete text path name

Txtcontent_texttree.value = This. nodename;
If (selectednodechanged! = NULL)
Selectednodechanged (sender, e );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.