vb.net TreeView 获得ID谢谢有源码

zq123zz zq123zz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
有个textbox1 我想点TreeView1的子节把ID传给textbox1中,谢谢帮一下!


Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Form2
Private ds As New DataSet()
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim CN As New SqlConnection()
Try

'初始化连接字符串

CN.ConnectionString = "Data Source=(local);Initial Catalog=yy;" & _
"Integrated Security=false;User ID=sa;Password=admin"
CN.Open()
'添加命令,从数据库中得到数据
Dim sqlCmd As New SqlCommand()
sqlCmd.Connection = CN
sqlCmd.CommandText = "select * from tbtree"
sqlCmd.CommandType = CommandType.Text
Dim adp As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
adp.Fill(ds)
Catch ex As Exception
MsgBox(ex.Message)
Finally
'关闭连接
CN.Close()
End Try
'调用递归函数,完成树形结构的生成
AddTree(0, Nothing)
End Sub
Private Sub AddTree(ByVal ParentID As Integer, ByVal pNode As TreeNode)
Dim Node As TreeNode
Dim dvTree As New DataView()
dvTree = New DataView(ds.Tables(0))
'过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "PARENTID = " + ParentID.ToString
Dim Row As DataRowView
For Each Row In dvTree
If pNode Is Nothing Then '判断是否根节点
'̀添加根节点
Node = TreeView1.Nodes.Add(Row("context").ToString())
'̀再次递归
AddTree(Int32.Parse(Row("ID").ToString()), Node)
Else
'添加当前节点的子节点
Node = pNode.Nodes.Add(Row("context").ToString())
'̀再次递归
AddTree(Int32.Parse(Row("ID").ToString()), Node)
End If
Node.EnsureVisible()
Next
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

End Sub
End Class
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
zq123zz
VB 16,718 成员 110,520 内容