惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Secure Thoughts
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
博客园 - Franky
有赞技术团队
有赞技术团队
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
NISL@THU
NISL@THU
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
W
WeLiveSecurity
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
O
OpenAI News
V
V2EX
AI
AI
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
美团技术团队
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 三生石上(FineUI控件)
G
GRAHAM CLULEY
月光博客
月光博客
T
Threatpost
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
爱范儿
爱范儿
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
N
News | PayPal Newsroom
Know Your Adversary
Know Your Adversary

博客园 - 寒天飞雪

项目中用到Excel上传到Sql数据库 转载: 房贷的两种还款方式介绍 RFC访问SAP(C#) VB.net连接SAP实例(vb.net写法) VB.net连接SAP实例 - 寒天飞雪 - 博客园 介绍: MRP和MPS 收藏: SQLServer的存储结构 转载: 正则表达式介绍 .net 连接ORACLE 数据库的例子 - 寒天飞雪 转载: ReportViewer : RDLC自定义工具栏 C#调用iTextSharp组件生成PDF文件, 在VS2005下已经调试通过! SQLSERVER 2005 BI的帮助文档说明: 9.2 定义和浏览翻译 9.1 定义和浏览透视 8.1 定义操作 7.1 定义关键指标KPI 6.3使用脚本命令定义作用域分配 6.2 定义命名集 6.1 定义计算成员
Winform 树型菜单例子 - 寒天飞雪 - 博客园
寒天飞雪 · 2008-12-29 · via 博客园 - 寒天飞雪

核心代码:

Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private ds As New DataSet()

    ' AddTree递归函数每次都要用到数据集中的一个表,所以定义成private
#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TreeView1
        '
        Me.TreeView1.ImageIndex = 0
        Me.TreeView1.ImageList = Me.ImageList1
        Me.TreeView1.Location = New System.Drawing.Point(0, 0)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = 0
        Me.TreeView1.Size = New System.Drawing.Size(272, 476)
        Me.TreeView1.TabIndex = 0
        '
        'ImageList1
        '
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        Me.ImageList1.Images.SetKeyName(0, "")
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(288, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "打开窗体二"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(288, 48)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(75, 23)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(376, 488)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TreeView1)
        Me.Name = "Form1"
        Me.Text = "用存储过程加载树"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' '定义数据库连接

        Dim CN As New SqlConnection()
        Try

            '初始化连接字符串
            CN.ConnectionString = "data source =255.255.255.255;initial catalog=baobiaopri;persist security info=False;user id=sa;Password=admin@sa;"
            CN.Open()
            '添加命令,从数据库中得到数据
            Dim sqlCmd As New SqlCommand()

            sqlCmd.Connection = CN
            Dim sql As String = ""
            'sql = "select '1' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '业务报表' as description ,  0 as parentid"
            'sql = sql + " union  all "
            'sql = sql + " select '2' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '处理过的表单' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '3' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '我提交的表单' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '4' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '其他可访问的表单' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '5' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '任务夹' as description ,  0 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '6' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '子子菜单' as description ,  1002 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '7' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '子子菜单' as description ,  6 as parentid"
            'sql = sql + " union all"

            'sql = sql + " select '8' as privilegeid , 'http://home.ghy.com.cn/ReportServer' as formname , '子子子菜单' as description ,  7 as parentid"
            'sql = sql + " union all"

            sql = sql + " select Privileges.PrivilegeID, Privileges.formname , Privileges.description, Privileges.ParentID  "
            sql = sql + " from Privileges inner join User_Privileges  on Privileges.Privilegeid = User_Privileges.Privilegeid"
            sql = sql + " where User_Privileges.userid = 'yinqingyue' order by Privileges.PrivilegeID"

            sqlCmd.CommandText = sql
            '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())
                Node = TreeView1.Nodes.Add(Row("Description").ToString())

                '再次递归
                AddTree(Int32.Parse(Row("PrivilegeID").ToString()), Node)
            Else
                '添加当前节点的子节点
                Node = pNode.Nodes.Add(Row("Description").ToString())
                '再次递归
                AddTree(Int32.Parse(Row("PrivilegeID").ToString()), Node)
            End If
            Node.EnsureVisible()
        Next
    End Sub
End Class