查找TreeView节点并展开节点所在的深度

方法如下:

protected void Button1_Click(object sender, EventArgs e)
        {

            //方法调用如下:
            //循环查找节点
            foreach (TreeNode node in this.treeView组织树.Nodes)
            {
                TreeNode treeNode = FindNode(node, txt节点值.Text);//txt节点值.Text要查找节点的VALUE值

                if (treeNode != null)
                {

                    this.treeView1.HideSelection = false;---------即使在是去焦点时仍突出显示

         treeView1.SelectedNode = treeNode; 
                   // treeNode.Selected = true;
                }
                //Response.Write(treeNode.ValuePath.Trim() + "-" + treeNode.Value + "-" + treeNode.Depth.ToString());
                TreeNode parNode = new TreeNode();

                int count =treeNode.Level;//treeNode.Depth + 1;
                for (int i = 0; i < count//treeNode.Depth + 1; i++)
                {
                    parNode = treeNode.Parent;
                    treeNode = treeNode.Parent;
                    parNode.Expand();
                }              

            }
        }


        /// <summary>
        /// 查找指定节点
        /// </summary>
        /// <param name="tnParent">节点</param>
        /// <param name="strValue">值</param>
        /// <returns></returns>
        private TreeNode FindNode(TreeNode tnParent, string strValue)
        {
            TreeNode treeNode2 = new TreeNode(); 
            if (tnParent == null) return null;
            if (tnParent.Value == strValue) return tnParent;
            if (tnParent.Depth > 0) tnParent.CollapseAll();
            TreeNode tnRet = null;
            foreach (TreeNode tn in tnParent.ChildNodes)
            {
                tnRet = FindNode(tn, strValue);
                if (tnRet != null) break;
            }
            return tnRet;



          //下面是我在.net2.0下的实际代码

           TreeNode treeNode2 = new TreeNode();
            if (tnParent == null) return null;
            if (tnParent.Text == strValue) return tnParent;
            if (tnParent.Level > 0) tnParent.Collapse();//tnParent.CollapseAll();
            TreeNode tnRet = null;
            foreach (TreeNode tn in tnParent.Nodes)//tnParent.ChildNodes)
            {
                tnRet = FindNode(tn, strValue);
                if (tnRet != null) break;
            }
            return tnRet;
 
       }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值