各位老师:最近我在实验怎样做BOM表,即产品结构目录树,碰到一些问题,求教:
1.目录树生成代码我用以下
Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim nd As WinForm.TreeNode
Dim dt As DataTable = DataTables("设计BOM表")
tr.StopRedraw()
tr.Nodes.Clear
tr.Nodes.Add("全部")
For Each dr As DataRow In dt.Select("","序号") '遍历一个集合或数组的全部成员
If dr.IsNull("序号") = False AndAlso dr("序号").Length = 4 Then
nd = tr.Nodes.Add(dr("序号"), dr("图名"))
Functions.Execute("AddChildren",nd,dt)
End If
Next
tr.ResumeRedraw()
内部函数AddChildren的代码
Dim nd As WinForm.TreeNode = args(0)
Dim dt As DataTable = args(1)
For Each dr As DataRow In dt.Select("","序号") '按科目代码顺序添加
Dim km As String = dr("序号")
If km.StartsWith(nd.name) AndAlso km.Length = nd.Name.Length + 2 Then
' Dim cd As Winform.TreeNode = nd.Nodes.Add(km,km & " " & dr("图名"))
Dim cd As Winform.TreeNode = nd.Nodes.Add(km, dr("图名"))
Functions.Execute("AddChildren",cd,dt)
End If
Next
这样执行生成目录树时,1600条左右耗时40秒左右,30000左右条数据时,电脑直接卡死,不知哪里出了问题。
2.目录树生成代码我用以下
Dim tr As WinForm.TreeView = e.Form.Controls("TreeView3")
Dim nd As WinForm.TreeNode '目录树节点
Dim dt As DataTable = DataTables("设计BOM表")
tr.StopRedraw() '停止绘制目录树 ResumeRedraw 恢复绘制目录树
tr.Nodes.Clear '清除所有节点
tr.Nodes.Add("全部")'增加一个节点
For Each dr As DataRow In dt.datarows '数据行的集合
If dr.IsNull("父级") Then '判断某一列是否为空
nd = tr.Nodes.Add(dr("序号"),dr("图名")) '新增目录树节点
Functions.Execute("AddChildren1",nd,dt) '增加自定义函数,名为AddChildren
End If
Next
tr.ResumeRedraw() '重新绘制目录树
内部函数AddChildren1的代码
Dim nd As WinForm.TreeNode = args(0)
Dim dt As DataTable = args(1)
Dim drs As List(of DataRow)
drs = dt.Select("[父级] = '" & nd.name & "'")
For Each dr As DataRow In drs
Dim cd As Winform.TreeNode = nd.Nodes.Add(dr("序号"),dr("图名"))
Functions.Execute("AddChildren1",cd,dt)
Next
这样生成目录树,数据在30000条左右时,耗时1分钟左右,请问这样正常吗,能否有更高效的方法,谢谢