2 allbluekos allbluekos 于 2015.06.02 10:18 提问

树状结构展示采购单Treeview 第二季

第一季没有得到答案,现在想到能否把父节点定义为 PO号,子节点为PO号+产品号,这样的话,每个子节点可以找到对应的父节点。。请高手列出详细方案,在线死等。
图片说明

5个回答

Tiger_Zhao
Tiger_Zhao   Rxr 2015.06.02 10:40
已采纳
    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 去查找父节点了。

caozhy
caozhy   Ds   Rxr 2015.06.02 10:26

treenode和此无关,首先它的nodes.Add方法你要会用。
至于你想显示什么,直接查询就可以了。
先用distinct查询出唯一的采购单号,然后再遍历每个采购单号,找到商品,添加到子节点上。
你可以将采购单的id查询出来放在节点的tag属性里。

caozhy
caozhy   Ds   Rxr 2015.06.02 10:27

建议你把完整的代码打包放在百度网盘,我才好给你具体的代码。

allbluekos
allbluekos   2015.06.02 10:34

我的代码是不正确的,因为思路都还没有,还是贴上去,请大家修正。谢谢
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
allbluekos
allbluekos   2015.06.02 11:26

图片说明
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
Tiger_Zhao
Tiger_Zhao 错误都没处理,有什么错都不知道。还有samid要第一个排序。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片