首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

VB.net中TreeView控件的使用案例

案例:通过代码读取sql server数据库中的信息来生成Treeview的目录节点,并通过点击节点实现信息的筛选查询。

首先在sql server数据库管理工具设计五个表,包括公司表Company、部门表Department、课题组表Team、实验室表LabToal、人员表WorkerTotal。其中Department、Team、LabToal、WorkerTotal表的结构见下图。

Department和Team表:

LabToal表:

WorkerTotal表:

然后打开Visual Studio2015,新建一个web网站,具体方法参见之前分享的文章。在default.aspx页面内,拖放Treeview控件,如图:

说明:Treeview控件的index排列如下,这与vb6不同。

解释:

如果要表示制定三级子节点3,代码:

TreeView1.Nodes(0).ChildNodes(1).ChildNodes(0)

要获取三级子节点3的文本

Str=TreeView1.Nodes(0).ChildNodes(1).ChildNodes(0).Text

要在三级子节点3添加4级节点:

TreeView1.Nodes(0).ChildNodes(1).ChildNodes(0).ChildNodes.Add(NewTreeNode(“四级子节点文本”))

然后在代码页面添加代码,连接sql server数据库前,需导入命名空间,在页面顶部,如下两句:

Imports System.Data

Imports System.Data.SqlClient

然后定义变量:

Dim conn As SqlConnection

Dim connstr As String = "Data Source=DG\SQLEXPRESS;Initial Catalog=Srici;User ID=sa;Password=sriciofdmwof1987"

Dim Comm As SqlCommand

Dim da As SqlDataAdapter, daTeam As SqlDataAdapter, daLab As SqlDataAdapter, daWorker As SqlDataAdapter

Dim ds As DataSet, dsTeam As DataSet, dsLab As DataSet, dsWorker As DataSet

Dim i As Int16, j As Int16, k As Int16

Protected Friend DepartmentNum As Int16 '部门数量

Dim DepartmentName() As String '单位部门名称

Dim TeamNum As Int16 '课题组数量

Dim TeamName() As String '某部门课题组名称

Dim LabNum As Int16 '实验室数量

Dim LabName() As String '某课题组实验门牌号

Dim WorkerNum As Int16 '人员数量

Dim WorkerName() As String ''某课题组组人员名字

然后在_Default_Load事件中,详细代码如下:

Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load

If Not IsPostBack Then '第一次打开页面

'连接服务器

conn = New SqlConnection(connstr)

conn.Open()

'添加单位根节点

da = New SqlDataAdapter("select * from Company", conn)

ds = New DataSet

da.Fill(ds, "MyCompany") '将数据库桥梁da填充到ds的MyCompany表中

TreeView1.Nodes.Add(New TreeNode(ds.Tables("MyCompany").Rows(0).Item(0).ToString))

'添加单位子节点/部门

da = New SqlDataAdapter("select * from Department", conn)

ds = New DataSet

da.Fill(ds, "MyDepartment")

DepartmentNum = ds.Tables("MyDepartment").Rows.Count

Session("DepartmentNum") = DepartmentNum '将部门数量传递给session

ReDim DepartmentName(DepartmentNum - 1)

For i = 1 To DepartmentNum

DepartmentName(i - 1) = ds.Tables("MyDepartment").Rows(i - 1).Item(1).ToString()

TreeView1.Nodes(0).ChildNodes.Add(New TreeNode(DepartmentName(i - 1))) '添加部门

Next i

Session("DepartmentName") = DepartmentName '将部门名字(数组)传递给session

'添加部门子节点/课题组

For i = 1 To ds.Tables("MyDepartment").Rows.Count

daTeam = New SqlDataAdapter("select * from Team Where Rtrim(部门)= '" & DepartmentName(i - 1) & "'", conn)

dsTeam = New DataSet

daTeam.Fill(dsTeam, "MyTeam")

TeamNum = dsTeam.Tables("MyTeam").Rows.Count

Session(i & "TeamNum") = TeamNum '将某部门课题组数量传递给session

ReDim TeamName(TeamNum - 1)

For j = 1 To TeamNum

TeamName(j - 1) = dsTeam.Tables("MyTeam").Rows(j - 1).Item(1).ToString

TreeView1.Nodes(0).ChildNodes(i - 1).ChildNodes.Add(New TreeNode(TeamName(j - 1))) '添加课题组

'读取添加课题组子节点/实验室

TreeView1.Nodes(0).ChildNodes(i - 1).ChildNodes(j - 1).ChildNodes.Add(New TreeNode("实验室"))

daLab = New SqlDataAdapter("select * from LabTotal Where Rtrim(课题组)= '" & TeamName(j - 1) & "'", conn)

dsLab = New DataSet

daLab.Fill(dsLab, "MyLab")

LabNum = dsLab.Tables("MyLab").Rows.Count

Session(i & j & "LabNum") = LabNum '将某部门某课题组实验室数量传递给session

ReDim LabName(LabNum - 1)

For k = 1 To LabNum

LabName(k - 1) = dsLab.Tables("MyLab").Rows(k - 1).Item(1).ToString

TreeView1.Nodes(0).ChildNodes(i - 1).ChildNodes(j - 1).ChildNodes(0).ChildNodes.Add(New TreeNode(LabName(k - 1)))

Next k

Session(i & j & "LabName") = LabName '将某部门某课题组实验室门牌号(数组)传递给session

'读取添加课题组子节点/人员

TreeView1.Nodes(0).ChildNodes(i - 1).ChildNodes(j - 1).ChildNodes.Add(New TreeNode("人员"))

daWorker = New SqlDataAdapter("select * from WorkerTotal Where Rtrim(课题组)= '" & dsTeam.Tables("MyTeam").Rows(j - 1).Item(1).ToString & "'", conn)

dsWorker = New DataSet

daWorker.Fill(dsWorker, "MyWorker")

WorkerNum = dsWorker.Tables("MyWorker").Rows.Count

Session(i & j & "WorkerNum") = WorkerNum '将某部门某课题组人员数量传递给session

ReDim WorkerName(WorkerNum - 1)

For k = 1 To WorkerNum

WorkerName(k - 1) = Trim(dsWorker.Tables("MyWorker").Rows(k - 1).Item(1).ToString) & "(" & Trim(dsWorker.Tables("MyWorker").Rows(k - 1).Item(2).ToString) & ")"

TreeView1.Nodes(0).ChildNodes(i - 1).ChildNodes(j - 1).ChildNodes(1).ChildNodes.Add(New TreeNode(WorkerName(k - 1)))

Next k

Session(i & j & "WorkerName") = WorkerName '将某部门某课题组人员名字(数组)传递给session

Next j

Session(i & "TeamName") = TeamName '将某部门所有课题组名字(数组)传递给session

Next i

conn.Close()

End If

End Sub

运行效果:

点击 Treeview节点,进行数据库的筛选查询,代码如下:

Protected Sub TreeView1_SelectedNodeChanged(sender As Object, e As EventArgs) Handles TreeView1.SelectedNodeChanged

TreeView1.SelectedNodeStyle.BackColor = System.Drawing.Color.Red'选中节点的背景色为红色

If TreeView1.SelectedNode.Text = TreeView1.Nodes(0).Text Then '点击单位根节点

SqlBindData("select * from 试剂 order by 更新日期 desc")

ElseIf TreeView1.SelectedNode.Parent.Text = TreeView1.Nodes(0).Text Then '点击部门节点

SqlBindData("select * from 试剂 where 部门 = '" & TreeView1.SelectedNode.Text & "'order by 更新日期 desc")

ElseIf TreeView1.SelectedNode.Parent.Parent.Text = TreeView1.Nodes(0).Text Then '点击课题组节点

SqlBindData("select * from 试剂 where 课题组 = '" & TreeView1.SelectedNode.Text & "'order by 更新日期 desc")

ElseIf TreeView1.SelectedNode.Parent.Text = "实验室" Then '点击实验室节点

SqlBindData("select * from 试剂 where 实验室 = '" & TreeView1.SelectedNode.Text & "'order by 更新日期 desc")

ElseIf TreeView1.SelectedNode.Parent.Text = "人员" Then '点击人员节点

SqlBindData("select * from 试剂 where 更新人 = '" & TreeView1.SelectedNode.Text & "'order by 更新日期 desc")

End If

End Sub

如筛选A-403实验室,运行效果如下:

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180626G1MNL600?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券