zoukankan      html  css  js  c++  java
  • C# 用数据库记录填充树

    在项目开发的过程中,树状结构用的很多,树结构不但能够很好的分类汇总,而且还能明确层次关系,方便操作

    这里写一个简单的C#填充数的程序,一般要填充树状结构就会用递归的方法

    先看数据库表结构:

     

     

    parentID为改name的父节点id

    C#代码:

     1  private void button1_Click(object sender, EventArgs e)
     2         {
     3             DataSet ds = GetData();
     4             if (ds.Tables.Count <= 0return;
     5             NodeAdd(treeView1, ds.Tables[0], true);
     6         }
     7 
     8         private static DataSet GetData()
     9         {
    10             using (SqlConnection con = new SqlConnection("data source=.;initial catalog=Test;user id=sa"))
    11             {
    12                 con.Open();
    13                 SqlCommand cmd = new SqlCommand("select * from Student", con);
    14                 SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    15                 DataSet ds = new DataSet();
    16                 adapter.Fill(ds);
    17                 return ds;
    18             }
    19         }
    20 
    21         //填充数
    22         private static void NodeAdd(TreeView tree, DataTable table, bool expandall)
    23         {
    24             foreach (DataRow row in table.Rows)
    25             {
    26                 string id = row["id"].ToString().Trim();
    27                 string name = row["name"].ToString().Trim();
    28                 string parentID = row["parentID"].ToString().Trim();
    29 
    30                 TreeNode node = new TreeNode(name);
    31                 node.Tag = id;
    32                 //在树中根据id来查找这个节点,如果没有找到,则说明这个节点是父节点,如果查找到这个节点,则返回,返回的节点为node节点的父节点
    33                 TreeNode parentNode = GetNodeByID(tree,parentID);
    34                 if (parentNode == null)
    35                 {
    36                     tree.Nodes.Add(node);
    37                 }
    38                 else  //如果不为空,则说明当前的节点为返回节点(parentNode)的子节点
    39                 {
    40                     parentNode.Nodes.Add(node);
    41                 }
    42                 if (expandall)
    43                 {
    44                     tree.ExpandAll();
    45                 }
    46             }
    47         }
    48 
    49         private static TreeNode GetNodeByID(TreeView tree, string id)
    50         {
    51             TreeNode result = null;
    52             foreach (TreeNode node in tree.Nodes)
    53             {
    54                 if (node.Tag.ToString() == id)
    55                 {
    56                     result = node;
    57                     break;
    58                 }
    59                 //判断当前节点是不是要查找的节点,如果不是,则查看当前节点是否有子节点,然后递归判断
    60                 if (node.Nodes.Count > 0)
    61                 {
    62                     result = GetNodeByID(node, id);
    63                     if (result != null)
    64                         break;
    65                 }
    66             }
    67             return result;
    68         }
    69 
    70         private static TreeNode GetNodeByID(TreeNode parentNode, string id)
    71         {
    72             TreeNode result = null;
    73             foreach (TreeNode node in parentNode.Nodes)
    74             {
    75                 if (node.Tag.ToString() == id)
    76                 {
    77                     result = node;
    78                     break;
    79                 }
    80 
    81                 if (node.Nodes.Count > 0)
    82                 {
    83                     result = GetNodeByID(node, id);
    84                     if (result != null)
    85                         break;
    86                 }
    87             }
    88             return result;
    89         }

    最后运行结果:

  • 相关阅读:
    MySQL日期比较
    MySQL日期函数、时间函数总结(MySQL 5.X)
    MySQL日期、字符串、时间戳互转
    通过 zxing 生成二维码
    前台时间格式 2019-03-09T16:00:00.000Z
    基于vue-cli配置手淘的lib-flexible + rem,实现移动端自适应
    python爬虫实例大全
    python3 BeautifulSoup模块使用
    Python 通过sgmllib模块解析HTML
    如何搭建一个合理的数值框架?
  • 原文地址:https://www.cnblogs.com/wangshuai/p/1782522.html
Copyright © 2011-2022 走看看