C # TreeView control uses Code _c# tutorial

Source: Internet
Author: User
Currently selected: Treeview.selectednode
Add top node: TreeView.Nodes.Add ("Key", "Text")
Increase sibling node: TreeView.SelectedNode.Parent.Nodes.Add ("Key", "Text")
Add child nodes: TreeView.SelectedNode.Nodes.Add ("Key", "Text")
Expand All: Treeview.expandall ()
All folds: Treeview.collapseall ()
Re-set the TreeView
database table structure is: ID type name Parent ID
Copy Code code as follows:

private void Loadtreeview ()
{
this. Category of goods Tableadapter1.fill (SUPERCARGODATASET1);
DataTable table = superCargoDataSet1. Goods category;
datarow[] row = table. Select ("Parent id=0");
foreach (DataRow r in row)
{
TreeNode node = Item 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. Goods 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 child nodes while deleting the corresponding records in the database
database table structure is: ID type name Parent ID
Copy Code code as follows:

private void Delete Toolstripbutton_click (object sender, EventArgs e)
{
if (item category Treeview.selectednode!= null)
{
datarow[] Rowchildren = superCargoDataSet1. Goods category. Select ("id=" + Category of Goods TreeView.SelectedNode.Name.ToString ());
if (Rowchildren!= null)
{
foreach (DataRow row in Rowchildren)
{
Delete node (row["ID"]. ToString ());
Row. Delete ();
}
}
Type of goods TreeView.SelectedNode.Remove ();
}
}
private void Delete node (string id)
{
datarow[] Rowchildren = superCargoDataSet1. Goods category. Select ("Parent id=" + ID);
if (Rowchildren!= null)
{
foreach (DataRow row in Rowchildren)
{
Delete node (row["ID"]. ToString ());
Row. Delete ();
}
}
}

TreeView Right-click to select
Copy Code code as follows:

private void TreeView left _mousedown (object sender, MouseEventArgs e)
{
if (E.button = = mousebuttons.right)
{
TreeNode node = left of TreeView. GetNodeAt (E.x, e.y);
if (node!= null)//Right key does not select the node, does not change the currently selected node. VS2005 so.
{
This.treeview to the left. SelectedNode = node;
}
}
}
Related Article

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.