treeview递归绑定的两种方法

Honoria ·
更新时间:2024-11-13
· 642 次阅读

方法一:

代码如下:
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);
  ;          }

您可能感兴趣的文章:基于MVC5和Bootstrap的jQuery TreeView树形控件(一)之数据支持json字符串、list集合基于mvc5+ef6+Bootstrap框架实现身份验证和权限管理Winform中Treeview实现按需加载的方法ASP.NET中使用TreeView显示文件的方法ASP.NET使用TreeView显示文件的方法Bootstrap树形控件使用方法详解基于MVC5和Bootstrap的jQuery TreeView树形控件(二)之数据支持json字符串、list集合



方法 treeview

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章