treeview递归绑定的两种方法

Posted ljs-13

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了treeview递归绑定的两种方法相关的知识,希望对你有一定的参考价值。

方法一:

 

复制代码代码如下:

public void creattree(int fid, TreeNode parentnode)
           {

               DataTable dt = new DataTable();
               dt = op.BindGroup();
               DataRow[] drs = dt.Select("parentid=" + fid);
               foreach (DataRow row in drs)
               {
                   if (parentnode != null)
                   {
                       TreeNode tempnode = new TreeNode();
                       tempnode.Text = row["groupname"].ToString();
                       tempnode.Name = row["groupid"].ToString();
                       parentnode.Nodes.Add(tempnode);
                       creattree(Convert.ToInt32(row["groupid"]), tempnode);
                   }
                   else
                   {
                       TreeNode tempnode = new TreeNode();
                       tempnode.Text = row["groupname"].ToString();
                       tempnode.Name = row["groupid"].ToString();
                       treeView1.Nodes.Add(tempnode);
                       creattree(Convert.ToInt32(row["groupid"]), tempnode);                    
                   }
               }
           }

           private void bindtree()
           {
               treeView1.Nodes.Clear();
               creattree(0,(TreeNode)null);
           }

 

方法二:

 

复制代码代码如下:

public void AddTree(int ParentID, TreeNode pNode)
           {
               DataTable dt = new DataTable();
               dt = op.BindGroup();
               DataView dvTree = new DataView(dt);
               //过滤ParentID,得到当前的所有子节点   
               dvTree.RowFilter = "parentid    =    " + ParentID;

               foreach (DataRowView Row in dvTree)
               {
                   TreeNode Node = new TreeNode();
                   if (pNode == null)
                   {          //添加根节点   
                       Node.Text = Row["groupname"].ToString();
                       treeView1.Nodes.Add(Node);
                       //Node.Expanded = true;

                       AddTree(Int32.Parse(Row["groupid"].ToString()), Node);          //再次递归   
                   }
                   else
                   {        //添加当前节点的子节点   
                       Node.Text = Row["groupname"].ToString();
                       pNode.Nodes.Add(Node);
                       //Node.Expanded = true;
                       AddTree(Int32.Parse(Row["groupid"].ToString()), Node);            //再次递归   
                   }
               }
           }

   private void bindtree()
           {
               treeView1.Nodes.Clear();
               AddTree(0, (TreeNode)null);
           }

以上是关于treeview递归绑定的两种方法的主要内容,如果未能解决你的问题,请参考以下文章

React 实现数据双向绑定 事件的绑定以及传参 获取表单值的两种方法

给网卡绑定多个ip的两种方法

WPF 数据绑定,界面刷新的两种方法-----INotifyPropertyChanged

Centos7网卡绑定的两种方法

js实现菲波那切数列的两种常用方法

Centos7网卡绑定的两种方法