Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim nd As WinForm.TreeNode = tr.SelectedNode
'MessageBox.show(tr.name)
'MessageBox.show(nd.name),经过测试,程序执行到这里没有出错
If nd IsNot Nothing Then
Dim bmid As Integer
Dim dr As DataRow = nd.DataRow '获取生成此节点的行
bmid = dr("id")
'MessageBox.show(bmid) 经过测试,程序执行不到这里就出错啦!找不到对象的实例.
Tables("员工基础表").Filter = "[部门ID] = " & bmid
If Tables("员工基础表").Rows.Count > 0
MainTable = Tables("员工基础表")
MessageBox.show("本部门内有员工,不能删除,只能更改部门名称,或先将部门内员工全部调出后才能删除!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning)
e.Cancel = True
MainTable = Tables("部门表")
Else
Dim pth() As String = nd.FullPath.Split("\")
Select Case nd.Level
Case 0
DataTables("部门表").DeleteFor("一级 = '" & pth(0) & "'")
Case 1
DataTables("部门表").DeleteFor("一级 = '" & pth(0) & "' And 二级 = '" & pth(1) & "'")
Case 2
DataTables("部门表").DeleteFor("一级 = '" & pth(0) & "' And 二级 = '" & pth(1) & "' And 三级 = '" & pth(2) & "'")
Case 3
DataTables("部门表").DeleteFor("一级 = '" & pth(0) & "' And 二级 = '" & pth(1) & "' And 三级 = '" & pth(2) & "' and 四级 = '" & pth(3) & "'")
End Select
nd.Delete()
End If
tr.Select()
End If
五、对于已有的节点,或者新增节点后执行代码刷新树形,删除按钮代码执行正常,节点和表中数据同步删除成功。