Winform tips-use the Treeview Control

Source: Internet
Author: User
Treeview Control
Function
The Treeview control displays the node hierarchy for users. It displays files and folders in the left pane of the Windows operating system resource manager. Each node in the tree view can contain other nodes. You can expand or collapse a node to display a parent node or a node that contains a child node. In addition, by setting the checkboxes attribute of the Tree View to true, you can display the check box next to the node. You can select or clear a node by setting the checked attribute of the node to true or false.
2. Attributes
Common attributes and descriptions of the Treeview control are shown in table 1.

Table 1 common attributes and descriptions of Treeview controls
The following describes in detail the nodes attribute, which is used to set all nodes in the Treeview control.
Syntax:
Public treenodecollection nodes {Get ;}
Property Value: treenodecollection, indicating the tree node allocated to the Tree View control. --------------------------------------------------------------------------- Selected item: Treeview. selectednode

Add a top-level node: Treeview. nodes. Add ("key", "text ")

Add peer nodes: Treeview. selectednode. Parent. nodes. Add ("key", "text ")

Add a subnode: Treeview. selectednode. nodes. Add ("key", "text ")

Expand All: Treeview. expandall ()

Close all: Treeview. collapseall ()

Reset Treeview

Database Table Structure: ID type name parent level ID
Copy the Code as follows:

Private void loadtreeview ()

{

This. item category tableadapter1.fill (supercargodataset1. item category );

Datatable table = supercargodataset1. item category;

Datarow [] ROW = table. Select ("parent id = 0 ");

Foreach (datarow R in row)

{

Treenode node = product category Treeview. nodes. Add (R ["ID"]. tostring (), R ["type name"]. tostring ());

Recursionshow (node, R ["ID"]. tostring ());

}

}

Private void recursionshow (treenode nodes, string ID)

{

Datatable table = supercargodataset1. item category;

Datarow [] ROW = table. Select ("parent id =" + id );

If (row! = NULL)

{

Foreach (datarow R in row)

{

Treenode node = nodes. nodes. Add (R ["ID"]. tostring (), R ["type name"]. tostring ());

Recursionshow (node, R ["ID"]. tostring ());

}

}

}



Delete the selected node and Its subnodes, and delete the corresponding records in the database.

Database Table Structure: ID type name parent level ID
Copy the Code as follows:

Private void Delete toolstripbutton_click (Object sender, eventargs E)

{

If (item category Treeview. selectednode! = NULL)

{

Datarow [] rowchildren = supercargodataset1. item category. Select ("ID =" + item category Treeview. selectednode. Name. tostring ());

If (rowchildren! = NULL)

{

Foreach (datarow row in rowchildren)

{

Delete node (row ["ID"]. tostring ());

Row. Delete ();

}

}

Product Category Treeview. selectednode. Remove ();

}

}

Private void delete node (string ID)

{

Datarow [] rowchildren = supercargodataset1. item category. Select ("parent id =" + id );

If (rowchildren! = NULL)

{

Foreach (datarow row in rowchildren)

{

Delete node (row ["ID"]. tostring ());

Row. Delete ();

}

}

}



Right-click Treeview and select
Copy the Code as follows:

Private void Treeview left _ mousedown (Object sender, mouseeventargs E)

{

If (E. Button = mousebuttons. Right)

{

Treenode node = Treeview left. getnodeat (E. X, E. y );

If (node! = NULL) // right-click the node that is not selected and does not change the selected node. This is true for vs2005.

{

This. Treeview left. selectednode = node;

}

}

}

Traverse the Treeview node (recursive algorithm)
Private void page_load (Object sender, system. eventargs E)
{
Getallnodetext (treeview1.nodes );
}
Void getallnodetext (treenodecollection TNC)
{
Foreach (treenode node in TNC)
{
If (node. nodes. Count! = 0)
Getallnodetext (node. nodes );
Response. Write (node. Text + "");
}
}

Obtain the parent node of the node.
Treenode pnode;
If (node. parent is treenode)
Pnode = (treenode) node. parent;
Else
// Node is Root Node

Modify the Treeview style (example)
<Iewc: Treeview id = "treeview1" runat = "server" hoverstyle = "color: Blue; Background: #00 ffcc;" defaultstyle = "Background: red; color: yellow; "selectedstyle =" color: red; Background: #00ff00; ">
Code:
Treeview1.defaultstyle ["font-size"] = "20pt ";

Not submitted during expansion. submitted only when node selection is changed
Set autopostback to false;
Add <body onload = "inittree ()">
Then write in pageload:
String strtreename = "treeview1 ";
String strref = page. getpostbackeventreference (treeview1 );
String strscript = "<script language =/" javascript/">/N" + "<! --/N "+" function inittree () {/N "+" + strtreename + ". onselectedindexchange = function () {/N" + "If (event. oldtreenodeindex! =
Event. newtreenodeindex)/n "+" this. queueevent ('onselectedindexchang', event. oldtreenodeindex + ',' + event. newtreenodeindex);/N "+" window. setTimeout ('"+ strref. replace ("'","//'")
+ "', 0, 'javascript '); /n "+"}/N "+"}/N "+" // -->/N "+" </SCRIPT> ";
Page. registerclientscriptblock ("inittree", strscript );
 
In this way, you can only submit the changed node!

Combine Treeview with XML
Set the XML file to the following format, and directly set treenodesrc to the XML file.
<? XML version = "1.0" encoding = "gb2312"?>
<Treenodes>
<Treenode text = "node0" expanded = "true">
<Treenode text = "node1"/>
<Treenode text = "node2"/>
</Treenode>
<Treenode text = "node3" navigateurl = "3. aspx"/>
</Treenodes>
Or use the code
Treeview1.treenodesrc = "A. xml ";
Treeview1.databind ();

 

1. Set the selected node. For example, select the second node.
Function setselnode ()
{
Treeview1.selectednodeindex = "1 ";
}

2. Get the text, ID, or nodedata of the selected node.
Function getattribute ()
{
Alert (treeview1.gettreenode (treeview1.selectednodeindex). getattribute ("text "));
}
Replace text with ID or nodedata to obtain the ID or nodedata of the selected node.

3. Modify node attributes, such as modifying the text of the first node
Function modifynode ()
{
VaR node = treeview1.gettreenode ("0 ");
Node. setattribute ("text", "hgknight ");
}

4. Click nodes.
Function treeview1.onclick ()
{
Alert (treeview1.gettreenode (treeview1.clickednodeindex). getattribute ("text "));
}

5. add nodes
Function addnode ()
{
VaR node = treeview1.createtreenode ();
Node. setattribute ("text", "hgknight ");
Treeview1.add (node );
}

6. js traverses all nodes
VaR allrootnode = new array ();
Allrootnode = treeview1.getchildren ();
Alertnode (allrootnode );

Function alertnode (nodearray)
{
If (parseint (nodearray. Length) = 0)
Return;
Else
{
For (I = 0; I <nodearray. length; I ++)
{
VaR cNode;
CNode = nodearray [I];
Alert (cNode. getattribute ("text "));
If (parseint (cNode. getchildren (). Length )! = 0)
Alertnode (cNode. getchildren ());

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.