The treeview node reads data from the database. How to modify, add, and delete the treeview Node

Source: Internet
Author: User
Treeview is one of the new Common display components (Common Control in COMCTL32.DLL) IN Win95. It also adds the corresponding Control Treeview from Delphi2.0 to replace the original Outline Control. Because of the complex structure of the tree structure, I often do not know how to start using it. Here we will introduce some problems in use.
Treeview is used to display data organized by tree structure, which is widely used in practice, such as a file system in a computer (Resource Manager in Windows95) the structure of an enterprise or a company. A tree chart in the Treeview control consists of a node (TreeNode) and a connection line. TtreeNode is the basic unit of TTreeview. A tree node contains Text and Data ). Text is a String type, and Data is an amorphous Pointer (Untyped Pointer), which can point to a Data structure associated with the node.
Each subnode under a node forms the Items attribute of this node. The current node has a unique Index (the Index attribute of TreeNode), which is used to indicate the position of the subnode in Items, the subnodes under each node are sequentially numbered, the first is 0, the second is 1, and so on. Use the IndexOf method to obtain the subnode sequence. The absolute order (AbsoluteIndex) refers to the sequence value starting from the first item of the Treeview, and the first value is 0, so it is pushed down. The Item attribute returns the Index subnode of the current node based on the Index value. Count indicates the number of all subnodes that belong to this item. Use the MoveTo method to move an Item from one position to another.
The Expanded attribute indicates whether all the sub-items are Expanded (including the sub-items). If it is True, all are Expanded. The IsVisible attribute indicates whether an Item can be seen in the tree. If all the fruit trees are expanded, this Item is certainly visible. The HasChildren attribute indicates whether an item has a subitem. GetFirstChild, GetLastChild, GetPrevChild, and GetNextChild return the first, last, first, and last items of the current item. GetNextSibling and GetPrevSibling return the next and previous items under the same Level. GetNextVisible and GetPrevVisible return the next and previous items that can be viewed. If a node has a Parent, the HasAsParent method returns True. Parent as the Parent of the current item. The Focused attribute determines whether the Focus falls on this node. When it is Focused, a standard box is enclosed. Obviously, only one node will be focused. The Selected Attribute indicates whether a node is Selected, and only one node is Selected. The DropTarget attribute indicates whether the node is the source or target in the drag operation.

1. add, delete, and modify nodes:
Static methods can be used to set the content of each node in the Items editor during design.
Make sure that a node is Selected (Treeview. Selected = nil) before adding or deleting a node)
Use AddFirst, AddFirstChild, and AddChild to add the Root node first, for example, Treeview. Items. AddFirst (nil, 'root ');
Then, add the subnode of this item.

Delete a node
Treeview. Selected. Delete

Edit node content
Treeview. Selected. EditText

Note: because the root node does not have a Parent node (TTreeNode. Parent = nil)
In addition, it is best to use it when adding large volumes of data to the Treeview
TreeView. Items. BeginUpdate;
Add Node
TreeView. Items. EndUpdate
This will speed up the display.

2. Add images to nodes
Several image-related attributes in Treeview:
SelectedIndex: Image selected in TimageList when the node is selected
OverlayIndex: select the secondary image as the hidden image (an image is transparently displayed in front of another image). For example, if a node is unavailable, add a secondary X image to the front.
ImageIndex: sequence number of the image selected in normal conditions
StateIndex: the sequence number corresponding to the StateImages ImageList. If the value is-1, the image is not displayed.
Typically, the Treeview control can display images before a node, as shown in the File Manager. Place an ImageList control in Form and add several images, which are respectively set to 0, 1 ,... Enter the name of the ImageList control you added in the Image attribute of the Treeview. The ImageIndex of TreeNode indicates the image sequence number when the node is not Selected (Selected = nil), and SelectedIndex indicates the image sequence number when the node is Selected.

3. About Level
The Level concept can be expressed as follows:
Level0 Level1 Level2

4. Sorting
SortType determines when to sort;
TreeView. AlphaSort sorts nodes. If you cannot meet the requirements, you can define your own CustomSort method.

5. The Drag & Drop operation is used in the same way as the standard Drag-and-Drop operation.
==========================================================
Let's talk about the second floor. Let's get some code for the landlord and the first floor.

Tree Structure:

Department staff // Root Node
|
| ______ Department 1
| _________ EMPLOYEE 1
| _________ EMPLOYEE 2
| _________ Employee 3
| ______ Department 2
| _________ EMPLOYEE 1
| _________ EMPLOYEE 2
| _________ Employee 3

Database Structure:
Bmdm bmmc xm bh
001 department 1 0
001 department 1 EMPLOYEE 1 0
001 department 1 Staff 2 1
002 Department 2 0
003 department 3 0
003 department 3 staff 2 0
003 department 3 staff 3 1

Create a table in the database, add the LoadTreeNode process, and call FormCreate.
The treeview must have a root node.

Procedure TfrmBMSZ. LoadTreeNode;
Var
RootNode, childNode: TTreeNode;
NodeBM: array of TTreeNode;
IBMCount, I: Integer;
SzBMDM, szBMMC, szZYMC: string; // Department staff name
SzTemp: string;
Begin
If not performance1.dataset. Active then Exit;
I: = 0;
RootNode: = Treeview1.Items [0];

// Add Department Information
With ADOQueryBM do
Begin
Close;
SQL. Add ('select bmdm, bmmc, xm from bmry where (bh = 0) order by bmdm ');
Open;
Last;
First;
IBMCount: = RecordCount;
If iBMCount> 0 then // number of departments
Begin
SetLength (NodeBM, iBMCount );
While not Eof do
Begin
SzBMDM: = FieldByName ('bmdm'). AsString;
SzBMMC: = szBMDM + ''+ FieldByName ('bmmc '). AsString;
NodeBM [I]: = Treeview1.Items. AddChild (rootNode, szBMMC );
NodeBM [I]. ImageIndex: = 1;
NodeBM [I]. SelectedIndex: = 1;
Inc (I );
Next;
End;
End;
Close;
End;

If I = 0 then Exit; // I = 0 indicates no Department Information


// Add employee information to the tree
I: =-1;
With performance1.dataset do
Begin
First;
While not Eof do
Begin
SzBMDM: = FieldByName ('bmdm'). AsString;
SzZYMC: = FieldByName ('xm '). AsString;
If (szBMDM <> szTemp) then
Begin
SzTemp: = szBMDM;
Inc (I );
End
Else
Begin
ChildNode: = Treeview1.Items. AddChild (NodeBM [I], szZYMC );
ChildNode. ImageIndex: = 2;
ChildNode. SelectedIndex: = 2;
End;
Next;
End; // while not Eof
Close;
End; // with ADOQuery2
Treeview1.FullExpand;
End;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.