首页后端开发ASP.NETvb点虐选择父结点 vbnet datagridview获取选中行

vb点虐选择父结点 vbnet datagridview获取选中行

时间2023-04-21 08:09:02发布访客分类ASP.NET浏览1043
导读:VB.NET TREEVIEW获取父节点 软糖来回答罗。treenode只有text和name两个string属性。所以给你写的函数是返回string数组,如需要可自行转换类型Integer Private Function GetP...

VB.NET TREEVIEW获取父节点

软糖来回答罗。

treenode只有text和name两个string属性。

所以给你写的函数是返回string数组,如需要可自行转换类型Integer

    Private Function GetParents(ByVal node As TreeNode) As String()

        Dim list As New List(Of String)

        Do Until node.Parent Is Nothing

            node = node.Parent

            list.Add(node.Text)

        Loop

        Return list.ToArray()

    End Function

下面是Listbox1的测试代码,把父节点都添加到Listbox1(在Click后)

    Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click

        Dim k = GetParents(TreeView1.Nodes(0).Nodes(0).Nodes(0).Nodes(0))

        ListBox1.Items.AddRange(k)

    End Sub

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

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点虐 递归 vb点虐 递归

这种不要用递归,知道关键字长度,知道节数用判断就可以了,取前3位第一节,第二节取3个为父节点,取全部为第二节关键字,第三节取前6个为父节点,取全部为第三节关键字.

并没有要求培养字段,查询时从len(ID)=3*N,每次循环时N都+1

select * from tablename where len(id)=3

select case N

case 1

treeview.node.add id,name

case 2

treeview.node(left(id,3)).node.add id,name

case 3

treeview.node(left(id,3)).node(left(id,6)).add id,name

end select

查询结果排个升序就不会存在这样的问题了,你刚才说的那种,如果中间没有比001001001001001更短的关键字,那么就脱节,这种是不可能添加到treeview中.

递归一般用在不知道节数,没有关键字的情况,比如系统目录结构.

递归整个C盘目录:

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

Dim iDir As IO.Directory

Dim node As New TreeNode

'先把C盘添加到树中

TreeView1.Nodes.Clear()

node.ImageIndex = 0

node.Text = "本地磁盘 C:"

node.SelectedImageIndex = -1

TreeView1.Nodes.Add(node)

Dim i As Integer

'获取C:盘根目录下的文件夹

Dim str() As String = IO.Directory.GetDirectories("D:\")

For i = 0 To str.GetUpperBound(0)

'调用遍历过程

AddDirectory("C:", str(i), node)

Next

node = Nothing

iDir = Nothing

End Sub

Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)

Dim iDir As IO.Directory

'Dim iDirInfo As IO.DirectoryInfo

Dim i As Integer

Dim node As New TreeNode

'先添加本目录,从文件夹路径分析出文件夹名称

node.Text = Strings.Replace(strPath, strFatherPath "\", "", , 1)

'为单个节点指定节点未被选中时显示的图标

node.ImageIndex = 1

'为单个节点指定节点被选中时显示的图标

node.SelectedImageIndex = 2

nodeFather.Nodes.Add(node)

Application.DoEvents()

Try

Dim str() As String = IO.Directory.GetDirectories(strPath)

'遍历该目录的子文件夹

For i = 0 To str.GetUpperBound(0)

AddDirectory(strPath, str(i), node)

Next

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

node = Nothing

iDir = Nothing

End Sub

VB.NET中如何把DataGridview中的数据,按数据父子关系添加到TreeView控件中?

用数据集操作更加直接。

具体来说,添加节点时指定节点KEY=ID(如果是数字ID前面加一个任意字母),并且把节点附加到父节点上(按上述规则添加很容易找到父节点),没有父节点则作为根节点。

应该能够看出添加时是按层级添加的,即先添加父节点后子节点,数据集可以先按层级排序。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: vb点虐选择父结点 vbnet datagridview获取选中行
本文地址: https://pptw.com/jishu/4371.html
c语言函数嵌套定义 c语言函数嵌套定义和嵌套调用的区别 c语言用函数求x的y次方 c语言pow函数求x的y次方的值

游客 回复需填写必要信息