问题描述
解决方案
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
'模拟数据(要求按采购单排序)
dt.Columns.Add("采购单", GetType(String))
dt.Columns.Add("产品", GetType(String))
dt.Rows.Add("PO150601", "铅笔")
dt.Rows.Add("PO150601", "橡皮擦")
dt.Rows.Add("PO150602", "笔记本")
dt.Rows.Add("PO150603", "")
'
Dim nd1 As TreeNode = Nothing
For Each dr As DataRow In dt.Rows
If (nd1 Is Nothing) OrElse (nd1.Name <> dr(0)) Then
nd1 = Me.TreeView1.Nodes.Add(dr(0), Mid(dr(0), 3))
End If
If Not String.IsNullOrEmpty(dr(1)) Then
nd1.Nodes.Add(dr(1), dr(1))
End If
Next
End Sub
注:VB.Net 中 TreeView 相同父节点下的兄弟是一个 Nodes 集合,没有全体节点的集合。所以 Key 不需要组合。
每个子节点的 Parent 就是父节点,不需要用 Key 去查找父节点了。
解决方案二:
treenode和此无关,首先它的nodes.Add方法你要会用。
至于你想显示什么,直接查询就可以了。
先用distinct查询出唯一的采购单号,然后再遍历每个采购单号,找到商品,添加到子节点上。
你可以将采购单的id查询出来放在节点的tag属性里。
解决方案三:
建议你把完整的代码打包放在百度网盘,我才好给你具体的代码。
解决方案四:
我的代码是不正确的,因为思路都还没有,还是贴上去,请大家修正。谢谢
Private Function SearchNode(ByVal Key As String) As TreeNode
If Key.Length = 1 Then
Return Me.TreeView1.Nodes(Key)
Else
Return SearchNode(Key.Substring(0, Key.Length - 2)).Nodes(Key)
End If
End Function
Private Sub SampleInst_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.Value = DateTime.Now.AddMonths(-1)
mycn.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=blue;uid=sa;pwd=0000"
mycommand.CommandText = " select samid,SamState,slState,(select procode from product(nolock) where Proid=Slproid) as Pcode,(select proName from product(nolock) where Proid=Slproid) as PName from Sample left join Samplelist on samid=slsamID and SamCdate between '" & DateTimePicker1.Value & "' and '" & DateTimePicker2.Value & "' and SamID like '%" & ComboBox1.Text & "%' order by samcdate desc,SamID desc,Pcode asc"
mycommand.Connection = mycn
Try
mycn.Open()
Dim mysqlreader As Data.SqlClient.SqlDataReader = mycommand.ExecuteReader
While mysqlreader.Read()
Dim key As String = mysqlreader.GetString(0)
Dim key1 As String = mysqlreader.GetString(0) & mysqlreader.GetString(3)
Dim text As String = mysqlreader.GetString(3) & "_" & mysqlreader.GetString(4)
If key = 1 Then
TreeView1.Nodes.Add(key, text)
Else
Dim parentkey As String = key.Substring(0, key.Length - 2)
SearchNode(parentkey).Nodes.Add(key, text)
End If
End While
Catch ex As Exception
Finally
mycn.Close()
End Try
End Sub
解决方案五:
Private Sub SampleInst_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker1.Value = DateTime.Now.AddMonths(-1)
mycn.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=blue;uid=sa;pwd=0000"
Dim mysql As String = " select samid,SamState,slState,(select procode from product(nolock) where Proid=Slproid) as Pcode,(select proName from product(nolock) where Proid=Slproid) as PName from Sample left join Samplelist on samid=slsamID and SamCdate between '" & DateTimePicker1.Value & "' and '" & DateTimePicker2.Value & "' and SamID like '%" & ComboBox1.Text & "%' order by samcdate desc,SamID desc,Pcode asc"
Dim mycom As New SqlClient.SqlCommand(mysql, mycn)
Dim dt As New DataTable
Try
mycn.Open()
myadapter.SelectCommand = mycom
myadapter.Fill(dt)
Dim nd1 As TreeNode = Nothing
For Each dr As DataRow In dt.Rows
If (nd1 Is Nothing) OrElse (nd1.Name <> dr(0)) Then
nd1 = Me.TreeView1.Nodes.Add(dr(0), Mid(dr(0), 1))
End If
If Not String.IsNullOrEmpty(dr(3)) Then
nd1.Nodes.Add(dr(3), dr(3))
End If
Next
Catch ex As Exception
Finally
mycn.Close()
End Try
End Sub