当前位置:网站首页>C # treeview tree structure recursive processing (enterprise group type hierarchical tree display)
C # treeview tree structure recursive processing (enterprise group type hierarchical tree display)
2022-07-19 10:27:00 【Enthusiasts who combine IT technology with enterprise applicati】
When doing the hierarchical display of enterprise groups ,c# The tree control of is particularly important , If there are too many levels , You need recursive processing .
1. This is a hierarchy , The first column is the unit code , The second column is the company name , The third column is the superior unit .
| dwbh | dwmc | sjgs |
| A000 | Huamei real estate group | |
| A001 | Huamei real estate group | A000 |
| A002 | Liaoning Huamei real estate company | A001 |
| A003 | Anhui Huamei real estate company | A001 |
| A004 | Sichuan Huamei real estate company | A001 |
| A005 | Jilin Huamei real estate company | A001 |
| A006 | Yunnan Huamei real estate company | A001 |
| A007 | Henan Huamei real estate company | A001 |
| A008 | Shenyang Huamei Branch | A002 |
| A009 | Hefei Huamei Branch | A003 |
| A010 | Dalian Huamei Branch | A002 |
| A011 | Chengdu Huamei Branch | A004 |
| A012 | Kunming Huamei Branch | A006 |
| A013 | Changchun Huanfen company | A005 |
| A014 | Shenhe real estate company | A008 |
| A015 | Dadong real estate company | A008 |
| A016 | Tiexi real estate company | A008 |
| A017 | Zhongshan real estate company | A010 |
2. adopt winform Tree control to display the list .


3. Code ( Here for intuitive understanding , Join the company line manually . In the implementation of the project, the data of the database is extracted .)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]{
new DataColumn("dwbh",typeof(string)),
new DataColumn("dwmc",typeof(string)),
new DataColumn("sjgs",typeof(string))});
DataRow r = dt.NewRow();
r[0] = string.Format("{0}", "A000");
r[1] = string.Format("{0}", " Huamei real estate group ");
r[2] = string.Format("{0}", "");
dt.Rows.Add(r);
DataRow r0 = dt.NewRow();
r0[0] = string.Format("{0}","A001");
r0[1] = string.Format("{0}", " Huamei real estate group ");
r0[2] = string.Format("{0}", "A000");
dt.Rows.Add(r0);
DataRow r1 = dt.NewRow();
r1[0] = string.Format("{0}","A002");
r1[1] = string.Format("{0}", " Liaoning Huamei real estate company ");
r1[2] = string.Format("{0}","A001");
dt.Rows.Add(r1);
DataRow r2 = dt.NewRow();
r2[0] = string.Format("{0}","A003");
r2[1] = string.Format("{0}", " Anhui Huamei real estate company ");
r2[2] = string.Format("{0}","A001");
dt.Rows.Add(r2);
DataRow r3 = dt.NewRow();
r3[0] = string.Format("{0}","A004");
r3[1] = string.Format("{0}", " Sichuan Huamei real estate company ");
r3[2] = string.Format("{0}","A001");
dt.Rows.Add(r3);
DataRow r4 = dt.NewRow();
r4[0] = string.Format("{0}","A005");
r4[1] = string.Format("{0}", " Jilin Huamei real estate company ");
r4[2] = string.Format("{0}","A001");
dt.Rows.Add(r4);
DataRow r5 = dt.NewRow();
r5[0] = string.Format("{0}","A006");
r5[1] = string.Format("{0}", " Yunnan Huamei real estate company ");
r5[2] = string.Format("{0}","A001");
dt.Rows.Add(r5);
DataRow r6 = dt.NewRow();
r6[0] = string.Format("{0}","A007");
r6[1] = string.Format("{0}", " Henan Huamei real estate company ");
r6[2] = string.Format("{0}","A001");
dt.Rows.Add(r6);
DataRow r7 = dt.NewRow();
r7[0] = string.Format("{0}", "A008");
r7[1] = string.Format("{0}", " Shenyang Huamei Branch ");
r7[2] = string.Format("{0}", "A002");
dt.Rows.Add(r7);
DataRow r8 = dt.NewRow();
r8[0] = string.Format("{0}", "A009");
r8[1] = string.Format("{0}", " Hefei Huamei Branch ");
r8[2] = string.Format("{0}", "A003");
dt.Rows.Add(r8);
DataRow r9 = dt.NewRow();
r9[0] = string.Format("{0}", "A010");
r9[1] = string.Format("{0}", " Dalian Huamei Branch ");
r9[2] = string.Format("{0}", "A002");
dt.Rows.Add(r9);
DataRow r10 = dt.NewRow();
r10[0] = string.Format("{0}", "A011");
r10[1] = string.Format("{0}", " Chengdu Huamei Branch ");
r10[2] = string.Format("{0}", "A004");
dt.Rows.Add(r10);
DataRow r11 = dt.NewRow();
r11[0] = string.Format("{0}", "A012");
r11[1] = string.Format("{0}", " Kunming Huamei Branch ");
r11[2] = string.Format("{0}", "A006");
dt.Rows.Add(r11);
DataRow r12 = dt.NewRow();
r12[0] = string.Format("{0}", "A013");
r12[1] = string.Format("{0}", " Changchun Huanfen company ");
r12[2] = string.Format("{0}", "a005");
dt.Rows.Add(r12);
DataRow r13 = dt.NewRow();
r13[0] = string.Format("{0}", "A014");
r13[1] = string.Format("{0}", " Shenhe real estate company ");
r13[2] = string.Format("{0}", "A008");
dt.Rows.Add(r13);
DataRow r14 = dt.NewRow();
r14[0] = string.Format("{0}", "A015");
r14[1] = string.Format("{0}", " Dadong real estate company ");
r14[2] = string.Format("{0}", "A008");
dt.Rows.Add(r14);
DataRow r15 = dt.NewRow();
r15[0] = string.Format("{0}", "A017");
r15[1] = string.Format("{0}", " Zhongshan real estate company ");
r15[2] = string.Format("{0}", "A010");
dt.Rows.Add(r15);
AddTree("A000", (TreeNode)null, dt);
}
public void AddTree(String ParentID, TreeNode pNode, DataTable table)
{
DataView dvTree = new DataView(table); //dtTree = dsFrame.Tables[0];
string Fstr = "sjgs='" + ParentID + "'";
dvTree.RowFilter = Fstr;
treeView1.ImageList = this.imageList1;
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null) // Processing master node
{
Node.Name = Row["dwbh"].ToString();
Node.Text = Row["dwbh"].ToString() + Row["dwmc"].ToString();
if ((Row["dwmc"].ToString()).Contains(" Gorgeous "))
{
Node.ImageIndex = 0;
Node.SelectedImageIndex = 0;
}
else if ((Row["dwmc"].ToString()).Contains(" Real estate "))
{
Node.ImageIndex = 2;
Node.SelectedImageIndex = 2;
}
treeView1.Nodes.Add(Node);
AddTree(Row["dwbh"].ToString(), Node, table); // recursive
}
else // Processing child nodes
{
Node.Name = Row["dwbh"].ToString();
Node.Text = Row["dwbh"].ToString() + Row["dwmc"].ToString();
if ((Row["dwmc"].ToString()).Contains(" Gorgeous "))
{
Node.ImageIndex = 0;
Node.SelectedImageIndex = 0;
}
else if ((Row["dwmc"].ToString()).Contains(" Real estate "))
{
Node.ImageIndex = 2;
Node.SelectedImageIndex = 2;
}
pNode.Nodes.Add(Node);
AddTree(Row["dwbh"].ToString(), Node, table);
}
}
}
}
}
边栏推荐
- R language uses R native functions to aggregate transforms, calculate window statistics, calculate min, and merge the generated statistical data into the original data set
- Good news
- English grammar_ Personal pronoun usage
- 2022年湖南省中职组“网络空间安全”赛题解析(超详细)
- HCIA rip experiment 7.11
- What is pytest? Automated testing is a must
- HCIA OSPF
- 机械臂速成小指南(零点五):机械臂相关资源
- Microsoft OneNote 教程,如何在 OneNote 中插入数学公式?
- HCIA review and answer 2022.7.6
猜你喜欢
随机推荐
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize dot strip plot, and set the add parameter to mean_ SD add the mean standard deviation vertical line and s
高性能IO框架库libevent(三):libevent框架函数概述
Solutions to notebook keyboard failure
荔枝音质高保真AI降噪技术分享
Go to school = earn money? Immortal college without paying tuition fees!
R语言使用R原生函数进行数据聚合统计(Aggregating transforms)计算滑动窗口统计值(Window Statistics)、计算滑动分组最小值(min)并合并生成的统计数据到原数据集
2022年湖南省中职组“网络空间安全”数据包分析infiltration.pacpng解析(超详细)
HCIA 静态综合实验报告 7.10
C语言自定义类型详解
智能存储柜控制系统设计及仿真
HCIA review and answer 2022.7.6
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
Quick completion guide of manipulator (zero five): resources related to manipulator
Ffmpeg merges multiple videos (vb.net, class library-8)
[Northeast Normal University] information sharing of postgraduate entrance examination and re examination
String type function transfer problem
新能源赛道已经高位风险,提醒大家注意安全
BEV空间内的特征级融合
[csp-j 2021] summary
Three.js基本元素使用


![[Mori city] random talk on GIS data (IV) - coordinate system](/img/e8/a09dac9eef768de7baee4125fe52f9.png)






