C#.NET TreeView.cs

 

        public void BindTreeView(DataTable dt,ref TreeView tvname)
        {
            //datatable.DefaultView.Sort("NodeSeq,ParentNode");
            tvname.Nodes.Clear();
            //创建根节点
            DataRow[] parentrow = dt.Select("ParentNode='0'", "NodeSeq");
            TreeNode rootnode = new TreeNode();
            rootnode.Text = parentrow[0][3].ToString();
            rootnode.Name = parentrow[0][4].ToString();//主键的值
            rootnode.Expand();
            //rootnode.Selected = true;
            tvname.Nodes.Add(rootnode);
            CreateChildNode(rootnode, dt);
        }


        public void CreateChildNode(TreeNode parentNode, DataTable datatable)
        {

            DataRow[] rowlist = datatable.Select("ParentNode='" + parentNode.Name.ToString() + "'", "NodeSeq,ParentNode");
            foreach (DataRow row in rowlist)
            {
                //创建新节点
                TreeNode node = new TreeNode();
                node.Name = row["NodeName"].ToString();
                node.Text = row["TreeNode"].ToString();
                node.Expand();
                //parentNode.ChildNodes.Add(node);
                parentNode.Nodes.Add(node);
                CreateChildNode(node, datatable);         //递归调用,寻找自身的子节点  
            }
        }

 

   客户端代码:           

          strSQL = "Select * from PAL_MES_TreeNode where AppName='PAL_MES' and FormName='frmSTReadyMaterial' and TreeName='tvLine'";
            DataTable datatable = CESQL.GetDataTable(strSQL);
            CCMRule.BindTreeView(datatable, ref tvLine);
            tvLine.ExpandAll();
            tvLine.SelectedNode = tvLine.Nodes[0].Nodes[0];
            tvLine.Focus();

 

         

你可能感兴趣的:(treeview)