treeview递归绑定 treeview递归绑定的两种方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

treeview递归绑定 treeview递归绑定的两种方法

  2021-03-19 我要评论
想了解treeview递归绑定的两种方法的相关内容吗,在本文为您仔细讲解treeview递归绑定的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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);
           }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们