Foxtable(狐表)用户栏目专家坐堂 → 用数据表保存分类的情况下目录树怎样增加同级?


  共有1802人关注过本帖树形打印复制链接

主题:用数据表保存分类的情况下目录树怎样增加同级?

帅哥哟,离线,有人找我吗?
huangxueyao
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:443 积分:3530 威望:0 精华:0 注册:2013/2/9 12:45:00
用数据表保存分类的情况下目录树怎样增加同级?  发帖心情 Post By:2016/12/27 18:12:00 [显示全部帖子]

http://help.foxtable.com/scr/2474.htm

在这个案例中,怎样写【增加同级】代码,当所选是根节点时,就增加同级根节点,当前节点是子节点时,就增加同级子节点。

我写了不少次代码,但是都不对~~


Dim tr As WinForm.TreeView
Dim nd As WinForm.TreeNode
tr = e.Form.Controls("TreeView1")
nd = tr.SelectedNode

If nd IsNot Nothing Then
    Dim dr As DataRow = DataTables("商品分类").AddNew
    'Dim id As Integer = nd.index
    If nd.Level = 0 Then
        dr("大类")="新节点"
        nd = tr.Nodes.Add("新节点")
    ElseIf nd.Level = 1
        dr("大类")=nd.ParentNode.text
        dr("小类")= "新节点"
        nd = nd.Nodes.Insert("新节点",0)
    End If
    
    tr.SelectedNode = nd
    tr.Select()
    tr.BeginEdit()
End If

当前节点是根节点时,就正确;
当前节点是子节点时,就不对……

求解答~


 回到顶部
帅哥哟,离线,有人找我吗?
huangxueyao
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:443 积分:3530 威望:0 精华:0 注册:2013/2/9 12:45:00
  发帖心情 Post By:2016/12/27 18:13:00 [显示全部帖子]

改add方法也不对

 回到顶部
帅哥哟,离线,有人找我吗?
huangxueyao
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:443 积分:3530 威望:0 精华:0 注册:2013/2/9 12:45:00
  发帖心情 Post By:2016/12/27 19:56:00 [显示全部帖子]

【追问】

5、“增加子节点”按钮的Click事件代码:

Dim tr As WinForm.TreeView
Dim
 nd As WinForm.TreeNode
tr = e.Form.Controls(
"TreeView1")
nd = tr.SelectedNode

If
 nd IsNot Nothing Then
    
If nd.Level = 2 Then
        MessageBox.Show(
"最多允许三层节点!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Else
        
Dim dr As DataRow = DataTables("表A").AddNew
        If
 nd.Level = 0 
Then
            dr(
"大类") = nd.Text
            dr(
"二类") = "新节点"
        
ElseIf nd.Level = 1
            dr(
"大类") = nd.ParentNode.Text
            dr(
"二类") = nd.text
            dr(
"三类") = "新节点"
        
End If
        nd = nd.Nodes.Add(
"新节点")
        tr.SelectedNode = nd
        tr.
Select()
        tr.BeginEdit()

    End
 
If
End
 If


上面这段代码会出现一个问题,因为前面增加根节点时,是通过addnew来实现的,现在增加子节点也是addnew,这样就会产生一行只有根节点,没有子节点的记录,应该怎样完善这个代码?


 回到顶部
帅哥哟,离线,有人找我吗?
huangxueyao
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:443 积分:3530 威望:0 精华:0 注册:2013/2/9 12:45:00
  发帖心情 Post By:2016/12/27 21:20:00 [显示全部帖子]

增加子节点:

Dim tr As WinForm.TreeView
Dim nd As WinForm.TreeNode
tr = e.Form.Controls("TreeView1")
nd = tr.SelectedNode
If nd IsNot Nothing Then
    If nd.Level = 1 Then
        MessageBox.Show("最多允许两级分类!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Else
        Dim dr As DataRow = DataTables("商品分类").AddNew
         If nd.Level = 0 Then
            dr("大类") = nd.Text
            dr("小类") = "新节点"
        'ElseIf nd.Level = 1
            'dr("大类") = nd.ParentNode.Text
            'dr("二类") = nd.text
            'dr("三类") = "新节点"
        End If
    End If
    nd = nd.Nodes.Add("新节点")
    tr.SelectedNode = nd
    tr.Select()
    tr.BeginEdit()
End If

我就是用示例代码改的,不知道哪里搞错了。


目录树AfterEditNode事件代码:
If e.NewText = "" Then
    e.Cancel = True
    Return
End If
Dim pth() As String = e.Node.FullPath.Split("\")
Select Case e.node.Level
    Case 0
        DataTables("商品分类").ReplaceFor("大类",e.NewText,"大类 = '" & pth(0) & "'")
    Case 1
        DataTables("商品分类").ReplaceFor("小类",e.NewText,"大类 = '" & pth(0) & "' And 小类 = '" & pth(1) & "'")
    'Case 2
        'DataTables("表A").ReplaceFor("三类",e.NewText,"大类 = '" & pth(0) & "' And 二类 = '" & pth(1) & "' And 三类 = '" & pth(2) & "'")
End Select
e.Node.Name = e.NewText

 回到顶部