假设有一个这样的树-根节点
 -子节点
   -孙节点
     -重孙节点
      -......假设我现在点击"重孙节点",我点击一个按钮。假设由一个ListBox控件显示:重孙节点
孙节点+重孙节点
子节点+孙节点+重孙节点
根节点+子节点+孙节点+重孙节点===============================================================================假设选择"孙节点",点击按钮。ListBox显示孙节点
子节点 + 孙节点
根节点 + 子节点 + 孙节点===================================================================================也就是说我选择一个节点后,怎样显示一共N条记录(N是当前选择的节点到根节点的层次)
第一条记录显示当前选择节点的名称,
第二条记录显示当前选择节点名称+选择节点的父节点名称
第三条记录显示当前选择节点名称+选择节点的父节点名称+选择节点的父节点的父节点的名称
......
直到根结点。不一定非要显示在ListBox中,只是求一个方法。谢谢大家

解决方案 »

  1.   

    TreeNode sn=TreeView.SelectedNode;
    if(sn==null)return;while((sn!=null)&&(sn.parent!=null))
    {
     showmessage(sn.text);
     sn=sn.parent;
    }//显示当前结点的子结点时,可能有多可,写一foreach可以搞定;
      

  2.   

    protected void Page_Load(object sender, EventArgs e) 
        { 
            if (IsPostBack == false) 
            {      
                PopulateRootLevel();//调用方法动态生成树 
            }              }    //获取并绑定数据源 
        private void PopulateRootLevel() 
        {         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString); 
            SqlCommand cmd = new SqlCommand(@"select Type.bT_ID,Type.bT_Name,count(distinct Left(book.book_ID,4)) 细类数目 from Type,Book where Left(book.book_ID,2)=Type.bT_ID group by Type.bT_ID ,Type.bT_Name", conn); 
            SqlDataAdapter ada = new SqlDataAdapter(cmd); 
            DataTable dt = new DataTable(); 
            ada.Fill(dt); 
            PopulateNodes(dt,TreeView1.Nodes); 
        } 
        //动态生成TreeView节点 
        private void PopulateNodes(DataTable dt, TreeNodeCollection nodes) 
        { 
            foreach(DataRow dr in dt.Rows) 
            { 
                TreeNode tn=new TreeNode(); 
                tn.Text = dr["bT_Name"].ToString(); 
                tn.Value = dr["bT_ID"].ToString(); 
                nodes.Add(tn); 
                tn.PopulateOnDemand = ((int)(dr["细类数目"]) > 0); 
            } 
        } 
        //生成子节点方法 
        private void PopulateSubLevel(int parentid, TreeNode parentNode) 
        { 
          
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString); 
            SqlCommand cmd = new SqlCommand("BTID ", conn); 
            cmd.CommandType = CommandType.StoredProcedure; 
            SqlParameter parameterBTID = new SqlParameter("@BTID",SqlDbType.Int); 
            parameterBTID.Value = parentid; 
            cmd.Parameters.Add(parameterBTID); 
            SqlDataAdapter ada = new SqlDataAdapter(cmd); 
            DataTable dt = new DataTable(); 
            ada.Fill(dt); 
            PopulateNodes(dt,parentNode.ChildNodes);     } 
        //触发生成子节点事件 
        protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e) 
        { 
            PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node); 
          
        } 
        } 
    试试看,我不久前实现的。应该能满足你的要求吧
      

  3.   

    这那里用的上递归啊   private void button1_Click(object sender, EventArgs e)
            {
                listBox1.Items.Clear();
                if (treeView1.SelectedNode != null)
                {
                    TreeNode _Node = treeView1.SelectedNode;
                    string _Text =_Node.Text;
                    while (_Node != null)
                    {
                        listBox1.Items.Add(_Text);
                        _Node = _Node.Parent;
                        if (_Node != null)
                        {
                            _Text = _Node.Text + "+" + _Text;
                        }
                    }
                }           
            }循环的方法...
      

  4.   

    用note的parent属性不就可以了么?
      

  5.   


                     List<string> pathArray;
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
    this.pathArray = new List<string>();
    getPath(e.Node);
    } void getPath(TreeNode node)
    {
    if (node == null)
    return; if (this.pathArray.Count == 0)
    this.pathArray.Add(node.Text);
    else
    this.pathArray.Add(this.pathArray[this.pathArray.Count - 1] + " - " + node.Text); getPath(node.Parent);
    }
      

  6.   

    ListItem li=new ListItem();
    TreeNode tn=TreeView.SelectNode;
    while(tn!=null)
    {
    li.Text+=item.Text;
    li.Value="...";
    lbx.Items.add(li)
    tn=tn.parent;
    }
      

  7.   

     if (treeView1.SelectedNode != null)
                {
                    treeView1.PathSeparator = "⊙";
                    string[] _Path = treeView1.SelectedNode.FullPath.Split('⊙');                for (int i = _Path.Length-1; i != -1; i--)
                    {
                        string _Value = "";
                        if (listBox1.Items.Count != 0) _Value=listBox1.Items[listBox1.Items.Count - 1].ToString();                    
                        listBox1.Items.Add(_Path[i]+"+"+_Value);
                    }
                }或则用STRING切分来解决