VB.NET中怎么给TreeView的父节点添加相对应的子节点?求助各位!

VB.NET中怎么给TreeView的父节点添加相对应的子节点?求助各位!,第1张

Public Class Form1

  Dim node(5) As TreeNode

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

      Dim root As TreeNode

      With TreeView1

          .Nodes.Clear()

          .ShowLines = True

          .ShowPlusMinus = True

          .ShowRootLines = True

          root = .Nodes.Add("仓库") '增加根节点

          .SelectedNode = root  '在此根节点下添加子节点

          For i = 1 To 6

              node(i - 1) = .SelectedNode.Nodes.Add(i.ToString & "号仓库")

          Next

          .ExpandAll()

      End With

  End Sub

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

      If Val(TextBox1.Text) >= 100 And Val(TextBox1.Text) <= 699 Then

          node(Val(TextBox1.Text) \ 100 - 1).Nodes.Add(TextBox1.Text)

      End If

  End Sub

  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

      If Val(TextBox2.Text) >= 1000000 And Val(TextBox2.Text) <= 6999999 Then

          For Each child As TreeNode In node(Val(TextBox2.Text) \ 1000000 - 1).Nodes

              If child.Text = TextBox2.Text.Substring(1, 3) Then

                  child.Nodes.Add(TextBox2.Text)

                  Exit For

              End If

          Next

      End If

  End Sub

End Class

我不会vb,我自己用C#写了一个,简单草参考一下吧:

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

this.Page.SmartNavigation = true

conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"])

conn.Open()

da = new SqlDataAdapter(@"select * from types", conn)

ds = new DataSet()

da.Fill(ds, "tree")

AddTree(0, (TreeNode)null)

conn.Close()

}

}

private void AddTree(int ParentID,TreeNode pNode)

{

DataView dvTree = new DataView(ds.Tables[0])

//过滤ParentID,得到当前的所有子节点

dvTree.RowFilter = "[ParentID] = " + ParentID

foreach (DataRowView Row in dvTree)

{

TreeNode Node = new TreeNode()

if (pNode == null)

{//添加根节点

Node.Text = Row["type"].ToString()

maplist.Nodes.Add(Node)

Node.Expanded = false

AddTree(Int32.Parse(Row["TID"].ToString()), Node) //再次递归

}

else

{ //添加当前节点的子节点

Node.Text = Row["type"].ToString()

pNode.ChildNodes.Add(Node)

Node.Expanded = false

AddTree(Int32.Parse(Row["TID"].ToString()), Node)//再次递归

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11266086.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存