1、teeView 控件的实例代码,需要的朋友可以参考下。增加顶级节点:TreeView.Nodes.Add(“Key“, “Text“)增加同级节点:TreeView.SelectedNode.Parent.Nodes.Add(“Key“, “Text“)增加子节点:TreeView.SelectedNode.Nodes.Add(“Key“, “Text“)全部展开:TreeView.ExpandAll()全部收拢:TreeView.CollapseAll()重新设置 TreeView数据库表结构为: ID 类型名称 父级 ID复制代码 代码如下:private void loadTreeVie
2、w()this.货品类别 TableAdapter1.Fill(superCargoDataSet1.货品类别);DataTable table = superCargoDataSet1.货品类别;DataRow row = table.Select(“父级 ID=0“);foreach (DataRow r in row)TreeNode node = 货品类别 TreeView.Nodes.Add(r“ID“.ToString(), r“类型名称“.ToString();recursionShow(node, r“ID“.ToString();private void recursionS
3、how(TreeNode nodes, string id)DataTable table = superCargoDataSet1.货品类别;DataRow row = table.Select(“父级 ID=“ + id);if (row != null)foreach (DataRow r in row)TreeNode node = nodes.Nodes.Add(r“ID“.ToString(), r“类型名称“.ToString();recursionShow(node, r“ID“.ToString();删除选中节点和其子结点,同时删除数据库中相应记录数据库表结构为: ID 类型
4、名称 父级 ID复制代码 代码如下:private void 删除 ToolStripButton_Click(object sender, EventArgs e)if (货品类别 TreeView.SelectedNode != null)DataRow rowChildren = superCargoDataSet1.货品类别 .Select(“ID=“ + 货品类别TreeView.SelectedNode.Name.ToString();if (rowChildren != null)foreach (DataRow row in rowChildren)delete 节点 (row
5、“ID“.ToString();row.Delete();货品类别 TreeView.SelectedNode.Remove();private void delete 节点(string id)DataRow rowChildren = superCargoDataSet1.货品类别.Select(“父级 ID=“ + id);if (rowChildren != null)foreach (DataRow row in rowChildren)delete 节点 (row“ID“.ToString();row.Delete();TreeView 右键选中代码如下:private void treeView 左侧_MouseDown( object sender , MouseEventArgs e )if( e.Button = MouseButtons.Right )TreeNode node = treeView 左侧 .GetNodeAt (e.X , e.Y);if( node != null )/右键未选中节点,不改变当前选中的节点。VS2005 如此。this.treeView 左侧.SelectedNode = node;