Treeview Control
The Treeview control displays the Hierarchy Structure of the Node object. Each Node object contains a label and an optional bitmap. The Treeview control is usually used to display document headers, entries in indexes, files and directories on disks, or various other information that can be displayed as hierarchical structures.
After creating the Treeview control, you can set the attributes of the Node object and call its method to add, delete, or manipulate the Node object. You can expand or contract nodes programmatically to show or hide all child nodes. The events collapse, expand, and nodeclick provideProgram.
You can use the root, parent, child, firstsibling, next, previous, and lastsibling attributes to check the references of the Node object.Code. At the root of the tree, select to jump to the tree header, and scroll the window if necessary.
The Treeview control has several options. A Node object can be expressed as one of the eight combinations of text, bitmap, line, and minus signs.
The Treeview control uses the imagelist control specified by the imagelist attribute to save the bitmap and icon used in the Node object. One Treeview control can only use one imagelist at a time. This means that when the style attribute of the Treeview control is set to display the image style, each member in the Treeview control displays an image of an equal size.
How to bind a Treeview to a database. The Code is as follows:
// Obtain the root node
Public void bindtree (INT tableid) // obtain the ID of a table from the database ,{
Dataset DS = new dataset ();
Using (sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["treeviewconnectionstring"]. connectionstring) // connect to the database
{
Sqlcommand COM = new sqlcommand ("select * from table where tableid =" + tableid, Conn );
Sqldataadapter da = new sqldataadapter (COM );
Da. Fill (DS );
}
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Treenode node = new treenode ();
Node. populateondemand = false;
Node. Text = Ds. Tables [0]. Rows [I] ["tablename"]. tostring ();
Node. value = Ds. Tables [0]. Rows [I] ["ID"]. tostring ();
Node. Target = "frmright ";
Node. navigateurl = Ds. Tables [0]. Rows [I] ["tableurl"]. tostring ();
Treeview1.nodes. Add (node );
Node. Expanded = false;
Node. selectaction = treenodeselectaction. Expand;
Bindchildnodes (node); // call the subnode Method
}
}
// Obtain the subnode
Public void bindchildnodes (treenode rootnode ){
Dataset DS = new dataset ();
Using (sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["treeviewconnectionstring"]. connectionstring ))
{
Sqlcommand COM = new sqlcommand ("select * from table where tableid =" + rootnode. Value, Conn );
Sqldataadapter da = new sqldataadapter (COM );
Da. Fill (DS );
}
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Treenode node = new treenode ();
Node. Text = Ds. Tables [0]. Rows [I] ["tablename"]. tostring ();
Node. value = Ds. Tables [0]. Rows [I] ["ID"]. tostring ();
Node. Target = "frmright ";
Node. navigateurl = Ds. Tables [0]. Rows [I] ["tableurl"]. tostring ();
Rootnode. childnodes. Add (node );
Bindchildnodes (node );
}
}
Finally, the method for obtaining the root node is called during page loading: bindtree (0 ).