zoukankan      html  css  js  c++  java
  • WinForm 数据库无限填充树目录 treeView

    我自己想的是处理数据库每一条数据,然后来插入子节点的子节点。

    奈何没有插入子节点的子节点的办法,百度来百度去,一看全都是递归。

    本来我是绝望的,

    但是没办法,老板的需求不能驳回啊,于是就来ctrl c/ctrl v吧。

    在网上查到了一个能看懂的,基本是原样copy了下来。

     1         private void SetTreeView()
     2         {
     3             //填充treeview
     4             string sql = "select * from SupplierType";
     5             DataTable dt = SQLHelp.GetDataTable(sql);
     6             dataGridView.DataSource = dt;
     7 
     8             if (dt != null)
     9             {
    10                 foreach (DataRow row in dt.Rows)
    11                 {
    12                     string id = row["SupplierTypeID"].ToString().Trim();
    13                     string name = "(" + id + ")" + row["SupplierTypeName"].ToString().Trim();
    14                     string fatherid = row["SupplierFatherTypeID"].ToString().Trim();
    15 
    16                     TreeNode node = new TreeNode(name);
    17                     node.Tag = id;
    18                     //在树中根据id来查找这个节点,如果没有找到,则说明这个节点是父节点,如果查找到这个节点,则返回,返回的节点为node节点的父节点
    19                     TreeNode parentNode = GetNodeByID(treeView, fatherid);
    20                     if (parentNode == null)
    21                     {
    22                         treeView.Nodes.Add(node);
    23                     }
    24                     else
    25                     {
    26                         parentNode.Nodes.Add(node);
    27                     }
    28                 }
    29             }
    30  
    31         }
    32 
    33         
    34         private TreeNode GetNodeByID(TreeView treeView, string fatherid)
    35         {
    36             //根据id搜索节点
    37             TreeNode result = null;
    38             foreach (TreeNode node in treeView.Nodes)
    39             {
    40                 if (node.Tag.ToString() == fatherid)
    41                 {
    42                     result = node;
    43                     break;
    44                 }
    45                 if (node.Nodes.Count > 0)
    46                 {
    47                     result = GetNodeByID(node, fatherid);
    48                     if (result != null)
    49                     { break; }
    50                 }
    51             }
    52             return result;
    53         }
    54 
    55         private TreeNode GetNodeByID(TreeNode parentNode, string fatherid)
    56         {
    57             //根据节点搜索子节点的节点
    58             TreeNode result = null;
    59             foreach (TreeNode node in parentNode.Nodes)
    60             {
    61                 if (node.Tag.ToString() == fatherid)
    62                 {
    63                     result = node;
    64                     break;
    65                 }
    66                 if (node.Nodes.Count > 0)
    67                 {
    68                     result = GetNodeByID(node, fatherid);
    69                     if (result != null)
    70                     { break; }
    71                 }
    72             }
    73             return result;
    74         }

    原理就是递归去寻找父元素,将加载好的节点向上层添加,

    数据库中重点的数据就是,自己的ID,父类的ID,以及内容。

    成果:

    参考:http://www.cnblogs.com/wangshuai/archive/2010/07/21/1782522.html



    转载请标明出处

    作者:AaXuan

    地址:http://www.cnblogs.com/Aaxuan

    知识共享许可协议

    本作品采用  知识共享署名 3.0 未本地化版本许可协议  进行许可。

  • 相关阅读:
    ibatis插入正确但查询不出数据的问题
    Java 动态代理机制分析及扩展--转
    java实现插入排序算法 附单元测试源码
    unix基础知识
    作为大数据和云计算学习的一个序吧
    Understanding JVM Internals---不得不转载呀
    回文推理
    java 正则表达式提取html纯文本
    OpenCV功能界面和示例
    【POJ3268】Silver Cow Party 最短
  • 原文地址:https://www.cnblogs.com/Aaxuan/p/6893056.html
Copyright © 2011-2022 走看看