没接触过treeNode.有高手指点下。从数据库取出人事资料自动划分节点
数据资料如下
select company,Depart,Team,group0,Group1,employeeno,[name] from hr2014..personal
------解决思路----------------------
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn As New SqlConnection("Data Source=sql.hh350.vhostgo.com;Initial Catalog=ceshidb;Integrated Security=False;uid=ceshiid;Password=ceshipw;")
cn.Open()
Dim sjb As New SqlCommand("select * from table_tvw", cn)
Dim rd As SqlDataReader
rd = sjb.ExecuteReader
TreeView1.Nodes.Clear()
Try
While rd.Read = True
If Len(rd("KID")) = 2 Then
TreeView1.Nodes.Add(rd("KID"), rd("名称"), rd("imgid"), "c1")'根节点
Dim myimg As System.Drawing.Image = Image.FromFile("D:\MLLZSJMB\image\ico\" & rd("imgid") & ".ICO")'图像元素
ImageList1.Images.Add(rd("imgid"), myimg)
ElseIf Len(rd("KID")) = 3 Then
TreeView1.Nodes.Item(0).Nodes.Add(rd("KID"), rd("名称"), rd("imgid"), "c1")'第一字结点
Dim myimg As System.Drawing.Image = Image.FromFile("D:\MLLZSJMB\image\ico\" & rd("imgid") & ".ICO")
ImageList1.Images.Add(rd("imgid"), myimg)
ElseIf Len(rd("KID")) = 4 Then
TreeView1.Nodes.Item(0).Nodes.Item(6).Nodes.Add(rd("KID"), rd("名称"), rd("imgid"), "c1")
Dim myimg As System.Drawing.Image = Image.FromFile("D:\MLLZSJMB\image\ico\" & rd("imgid") & ".ICO")
ImageList1.Images.Add(rd("imgid"), myimg)
Else
TreeView1.Nodes.Item(0).Nodes.Item(6).Nodes.Item(4).Nodes.Add(rd("KID"), rd("名称"), rd("imgid"), "c1")
Dim myimg As System.Drawing.Image = Image.FromFile("D:\MLLZSJMB\image\ico\" & rd("imgid") & ".ICO")
ImageList1.Images.Add(rd("imgid"), myimg)
End If
Dim img As System.Drawing.Image = Image.FromFile("D:\MLLZSJMB\image\ico\c1.ICO")
ImageList1.Images.Add("c1", img)
End While
Catch ex As Exception
End Try
cn.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.TreeView1.ExpandAll()'展开
Me.TextBox1.Text = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.TreeView1.CollapseAll()'折叠
End Sub
End Class
------解决思路----------------------
还是递归逻辑的问题,你已经在循环里添加到了Team,那么你再把Node.Nodes去递推,取到的Node.Tag是你的Team,这时候你sql比较确实StrSQL = " Where depart='" & Node.Tag & "'",所以如果无法正确使用递归,你完全可以依照你添加depart和Team的逻辑,在Team循环内执行sql,检索出每个Team下的系级,然后添加,在级别固定的前提下,可以考虑不适用递归