当前位置: 编程技术>.net/c#/asp.net
C# TreeView控件实例代码(重新设置TreeView、删除节点、右键选中等)
来源: 互联网 发布时间:2014-08-30
本文导语: treeview可以做到: 当前选中项:TreeView.SelectedNode 增加顶级节点:TreeView.Nodes.Add("Key", "Text") 增加同级节点:TreeView.SelectedNode.Parent.Nodes.Add("Key", "Text") 增加子节点:TreeView.SelectedNode.Nodes.Add("Key", "Text") 全部展开:TreeView.ExpandAll()...
treeview可以做到:
当前选中项:TreeView.SelectedNode
增加顶级节点:TreeView.Nodes.Add("Key", "Text")
增加同级节点:TreeView.SelectedNode.Parent.Nodes.Add("Key", "Text")
增加子节点:TreeView.SelectedNode.Nodes.Add("Key", "Text")
全部展开:TreeView.ExpandAll()
全部收拢:TreeView.CollapseAll()
1、重新设置TreeView
数据库表结构为: ID 类型名称 父级ID
代码示例:
private void loadTreeView()
{
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 recursionShow(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());
}
}
}
{
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 recursionShow(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());
}
}
}
2、删除选中节点和其子结点,同时删除数据库中相应记录
数据库表结构为: ID 类型名称 父级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["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();
}
}
}
{
if (货品类别TreeView.SelectedNode != null)
{
DataRow[] rowChildren = superCargoDataSet1.货品类别.Select("ID=" + 货品类别TreeView.SelectedNode.Name.ToString());
if (rowChildren != null)
{
foreach (DataRow row in rowChildren)
{
delete节点(row["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();
}
}
}
3、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;
}
}
}
{
if( e.Button == MouseButtons.Right )
{
TreeNode node = treeView左侧.GetNodeAt (e.X , e.Y);
if( node != null )//右键未选中节点,不改变当前选中的节点。VS2005如此。
{
this.treeView左侧.SelectedNode = node;
}
}
}