C # code for using the TreeView Control

Source: Internet
Author: User

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 codeThe Code is 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 IDCopy codeCode: 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 selectCopy codeCode: 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;
}
}
}

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.