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;
}
}
}