Recursive example (2): Application of Treeview in winform-bind a region tree

Source: Internet
Author: User

There are many trees in C. For example, either Windows form programming or web programming has a control called Treeview. The Treeview control is a control that displays a tree structure, which is similar to the Tree Structure in Windows Resource Manager. The difference is that Treeview can be composed of any number of node objects. Each node object can be associated with text and images. In addition, the Treeview node in Web programming can also be displayed as a hyperlink and associated with a URL. Each node can also contain any number of subnode objects. The hierarchy that contains nodes and Their subnodes forms the tree structure presented by the Treeview control.

The following is a typical example: Use Treeview to bind data. Data is generally in a tree structure, such as the relationship between administrative areas, the relationship between company departments and department employees, and the relationship between disk directory files.

 

 

 

The relationship between the parent and child level is one-to-many. Therefore, in the database design, a field is often used as the foreign key of the primary key of the table, representing the parent Region ID. Of course, if you want to easily find an algorithm (for example, list all sub-regions in Wuhan), you can add another field to record the node ID from the root node to the current node.

Train of Thought Analysis:

1. obtain all the data in the table area and store it in the able.

2. Obtain the data of the root node and add it to the root node. The processing of the root node is often different from that of the child node. For example, the addition of the root node is in treeview1.nodes. add, and the child node recursion is to add on the parent node, so it is often necessary to separate processing. You can use datatable. Select ("fareaid =-1") to obtain the root node data. When binding the node. set text to the region name, node. the tag is set to the datarow or Region ID of the Data row corresponding to the region. In this way, the parent node region information is displayed when the sub-region is traversed, and the application can conveniently obtain the data corresponding to the selected node.

3. recursively traverse the child area and add it to the Treeview control. The recursive method parameter is node. The parent node. Tag can obtain the data information of the parent region, and then obtain its subregion.

Datarow [] rows = able. Select ("fareaid =" + parent Region ID ). After obtaining the sub-region, bind the obtained information to the newly created Node object. The method is the same as step 2, and then call yourself recursively. When the region does not contain any sub-region, recursive termination, that is, rows. Length = 0.

The Code is as follows:

 

Public partial class bindareaform: Form
{
Private datatable dt = NULL;

Public bindareaform ()
{
Initializecomponent ();
Initdatatable ();

}

// Obtain the data used by the Area
Private void initdatatable ()
{
Sqlconnection conn = new sqlconnection ("Data Source =.; initial catalog = test; Integrated Security = true ");
Sqlcommand cmd = new sqlcommand ("select * from Area", Conn );
Sqldataadapter Ada = new sqldataadapter (CMD );
Dt = new datatable ();
Ada. Fill (DT );
}

Private void bindareaform_load (Object sender, eventargs E)
{
Bindroot ();
}

// Bind the root node
Private void bindroot ()
{
Datarow [] rows = DT. Select ("fareaid =-1"); // get the root
Foreach (datarow Drow in rows)
{
Treenode rootnode = new treenode ();
Rootnode. Tag = Drow;
Rootnode. Text = Drow ["areaname"]. tostring ();
Treeview1.nodes. Add (rootnode );

Bindchildareas (rootnode );
}

}

// Recursively bind the subarea
Private void bindchildareas (treenode fnode)
{
Datarow DR = (datarow) fnode. Tag; // data row associated with the parent node
Int fareaid = (INT) Dr ["ID"]; // parent node ID
Datarow [] rows = DT. Select ("fareaid =" + fareaid); // subarea
If (rows. Length = 0) // recursive termination, when the region does not contain a subregion
{
Return;
}

Foreach (datarow Drow in rows)
{
Treenode node = new treenode ();
Node. Tag = Drow;
Node. Text = Drow ["areaname"]. tostring ();

// Add a subnode
Fnode. nodes. Add (node );
// Recursion
Bindchildareas (node );
}
}
}

Run:

 

 

 

 

Code: http://files.cnblogs.com/sndnnlfhvk/TViewSource.rar

Recursive example (a): traversing Binary Tree http://www.cnblogs.com/sndnnlfhvk/archive/2011/03/31/2001015.html
Recursive example (2): Application of Treeview in winform-binding area tree http://www.cnblogs.com/sndnnlfhvk/archive/2011/03/31/2001064.html
Recursive example (III): applications of Treeview in winform-bind disk directory (I) http://www.cnblogs.com/sndnnlfhvk/archive/2011/03/31/2001065.html
Recursive example (4): applications of Treeview in winform-bind disk directory (2) http://www.cnblogs.com/sndnnlfhvk/archive/2011/03/31/2001072.html

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.