Henry Instructions - VB.NET Dynamic Load TreeView Node (1)

zhaozj2021-02-16  49

Henry Instructions - VB.NET Dynamic Load TreeView Node (1)

Han Rui (2003.4.13)

TreeView is an important control, either in VB.NET, C # or Delphi, VC , etc., acts as a navigation role. In actual work, many cases need to connect TreeView with the database to fill its node. However, often due to numerous database data, all root nodes and subpost are possible at one time, consume a lot of starting waiting time. The solution should be dynamically loaded nodes. When it starts, only the root node is loaded. When you click on a root node, then the child node is loaded, and then the child node is added to the child node when you click on a child node. Such push, such processing methods can achieve improved work efficiency and save access time.

This article is only VB.NET as an example, indicating the use of this method. Other programming languages ​​can be imitated or write to the author.

It should first be a database to save the data structure and data. The database required for this article is a project.mdb. Its structure is (for the sake of clarity, as an example of the Chinese character field, please change your own):

Table name: root node

Field

Types of

size

Primary key

Root node number

text

10

Y

Root node name

text

10

Table name: First level child node

Field

Types of

size

Primary key

Root node number

text

10

Y

First level child node number

text

10

Y combined primary key

First level child node name

text

10

Table name: secondary child node

Field

Types of

size

Primary key

First level child node number

text

10

Y

Second level child node number

text

10

Y combined primary key

Second level child node name

text

10

The relationship between three tables is very clear, there is no longer listening. The data filled in the table is:

The root node number is 1, 2, 3 so that the name is the root node 1, root node 2, root node 3

The first level child node number rule is: the child node number below root node is 11, 12, 13, etc., the child node number below the root node 2 is 21, 22 ... The child node name is: Node 1, child node 2 ...

The second level sub-node number rule is: the number of the second level node under the first grade child node ij is IJ1, IJ2 ..., its name is unified: Sun node 1, Sun node 2 ...

Let us now analyze the structure of TreeView (the author is ready to analyze the structure of TreeView in additional "to facilitate interested netizens)

TreeView is composed of nodes Treenode, the first level is called root node Treeroot, and the next level of the next level of the root node TreeEleaf, the child's node under a child node A child node called the child node. The first node has two identification methods, one is its text, that is, the displayed content; the other is its TAG property, which is generally identified with a unique identification code for identification of nodes in use. In this article, the name field of the node is also mainly used, and the Node's number attribute is displayed with the Tag property. (The node number is set to the primary key, that is, the only ID)

Add root nodes

Ok, we should start driving in VB.NET! The first step, of course, look at how to load root nodes when the form starts:

'Define public variables

DIM MyConnection As New OLEDB.OLEDBCONNECTION ()

Dim myadaPater as new oledb.oledbdataadapter () Dim MyCommand as new oledb.oledbcommand ()

DIM DS AS New DataSet ()

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

'Load root node table to TreeView, as the first level

MyConnection.connectionstring = "provider = microsoft.jet.Oledb.4.0; data source =" & application.startuppath & "/project.mdb" Database connection Please replace

MyCommand.commandtext = "SELECT Root Node No., Root Name" from root node "

mycommand.connection = myconnection

Try

MyConnection.Close ()

MyConnection.Open ()

DIM mysqlreader as oledb.oledbdatareader = mycommand.executeReader

TreeView1.nodes.clear ()

While MySqlreader.Read ()

Dim Tree_Root As New Treenode ()

Tree_root.tag = mysqlreader.getstring (0) 'put the number into the TAG

Tree_root.text = mysqlreader.getstring (1) 'The tree is displayed on the root node name

'Please decide whether to use getString or other type according to the type of your database field.

TreeView1.nodes.add (Tree_Root)

End while

Catch exception

Messagebox.show (ex. TString, "Data Sheet Renal Node Map", Vbokonly)

Finally

MyConnection.Close ()

END TRY

TreeView1.expandall ()

TreeView1.select ()

End Sub

Ok, now run a program, you can see the starting interface as shown in Figure 1

----

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

QQ: 18349592

E-mail: Henry7685@hotmail.com

Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/

转载请注明原文地址:https://www.9cbs.com/read-25544.html

New Post(0)