老师这段代码如何优化,不是生成多行再删除,而是仅生成一条数据
Dim dt As DataTable = DataTables("department")
Dim dic As New Dictionary(of Integer, Integer)
Dim prevLevel As Integer = 0
Dim idxs As String = "-1,"
For Each nd As WinForm.TreeNode In e.Form.Controls("TreeView1").AllNodes
If nd.Level = 0 AndAlso dic.count > 0 Then
Dim temp = dic(0)
dic.Clear
dic.Add(0, temp)
End If
If dic.ContainsKey(nd.Level) = False Then
dic.Add(nd.Level, 1)
Else
dic(nd.Level) = dic(nd.Level) + 1
End If
For i As Integer = nd.level + 1 To prevLevel
dic(i) = 0
Next
prevLevel = nd.Level
Dim ndr As DataRow
If nd.DataRow IsNot Nothing Then
ndr = dt.Find("_Identify = " & nd.DataRow("_Identify"))
Else
ndr = dt.AddNew
End If
idxs &= ndr("_Identify") & ","
Dim str As String = ""
For i As Integer = 0 To nd.Level
str &= format(dic(i), "000") & "."
Next
ndr("departno") = str.TrimEnd(".")
ndr("department") = nd.Text
Next
dt.DeleteFor("_Identify not in (" & idxs & ")")