Summary: In VB6, the TreeView is often used to represent hierarchical data, but the related code that interacts with the database requires a lot of manual coding; In vb.net, because of the enhancement of the data binding function and the enhancement of language features, it is easy to bind the TreeView to the hierarchical data, this article will first establish a dbtreeview that inherits from the TreeView and then use a unit (department) 's hierarchical data is bound to Dbtreeview and provides code to interact with the database.
1, from the level of data to express the way to start
In this example, the Department table (Department) has five fields, such as the following table:
Field name |
Field |
Type description |
Id |
Automatic numbering |
Key |
Code |
String |
Coding |
Name |
String |
Name |
Pid |
Int |
ID of parent Node |
CPtr |
Boolean |
Do you have any child nodes? |
2, inherit from the TreeNode Mytreenode
In Mytreenode, three new properties are added, such as the following table:
Property name |
Type |
Description |
Value |
Object |
Key |
Pid |
Object |
ID of parent Node |
CPtr |
Boolean |
Do you have any child nodes? |
In the Init event, set the three properties and the Text property, based on the four parameters passed in.
3, bind the Dbtreeview to the data source
Property name |
Type |
Description |
Datasource |
DataView |
Dbtreeview data sources use DataView instead of object |
Value |
Member |
String value member (column name of data source [DataView]) |
Display |
Member |
string display (in text) member |
Pid |
Member |
String Parent ID Member |
CPtr |
Member |
Whether string has child nodes |