//可手动添加根节点和子节点,也可代码添加 //两个根节点 TreeNode tn1 = treeView1.Nodes.Add("省份"); TreeNode tn2 = treeView1.Nodes.Add("市区"); //三个子节点,用于显示省份 TreeNode tn01 = new TreeNode("河南省"); TreeNode tn02 = new TreeNode("江苏省"); TreeNode tn03 = new TreeNode("浙江省"); //三个子节点,用于显示市区 TreeNode tn04 = new TreeNode("郑州市"); TreeNode tn05 = new TreeNode("南京市"); TreeNode tn06 = new TreeNode("杭州市"); tn1.Nodes.Add(tn01); tn1.Nodes.Add(tn02); tn1.Nodes.Add(tn03); tn2.Nodes.Add(tn04); tn2.Nodes.Add(tn05); tn2.Nodes.Add(tn06);
private void Form1_Load(object sender, EventArgs e) { //可手动添加根节点和子节点,也可代码添加 //两个根节点 TreeNode tn1 = treeView1.Nodes.Add("省份"); TreeNode tn2 = treeView1.Nodes.Add("市区"); //三个子节点,用于显示省份 TreeNode tn01 = new TreeNode("河南省"); TreeNode tn02 = new TreeNode("江苏省"); TreeNode tn03 = new TreeNode("浙江省"); //三个子节点,用于显示市区 TreeNode tn04 = new TreeNode("郑州市"); TreeNode tn05 = new TreeNode("南京市"); TreeNode tn06 = new TreeNode("杭州市"); tn1.Nodes.Add(tn01); tn1.Nodes.Add(tn02); tn1.Nodes.Add(tn03); tn2.Nodes.Add(tn04); tn2.Nodes.Add(tn05); tn2.Nodes.Add(tn06); //给节点添加图标 imageList1.Images.Add(Image.FromFile(@"F:\图片\Pictures\立体图片\Fish06.jpg")); imageList1.Images.Add(Image.FromFile(@"F:\360wallpaper_dt.jpg")); treeView1.ImageList = imageList1; treeView1.ImageIndex = 0; tn01.ImageIndex = 1; //选中后换图标 treeView1.SelectedImageIndex = 1; } private void button1_Click(object sender, EventArgs e) { treeView1.Nodes.Remove(treeView1.SelectedNode); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { label2.Text = e.Node.Text; }
将数据库中的数据递归加载到TreeView上
class Area { public int AreaID { get; set; } public string AreaName { get; set; } public int AreadPID { get; set; } }
class SqlHelper { private static readonly string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; /// <summary> /// 执行SQL语句返回一个DataReader /// 当返回DataReader的时候 注意 /// 1、Connection不能关闭 /// 2、DataReader不能关闭 /// 3、Command对象执行ExecuteReader()方法的时候传递一个参数ConmandBehavior.CloseConnection /// </summary> /// <param name="sqlCmd"></param> /// <param name="pms"></param> /// <returns></returns> public static SqlDataReader ExecuteReader(string sqlCmd, params SqlParameter[] pms) { SqlConnection con=new SqlConnection(conStr); using (SqlCommand cmd=new SqlCommand(sqlCmd,con)) { if (pms != null) { cmd.Parameters.AddRange(pms); }con.Open(); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return reader; } } public static int ExecuteNonQuery(string sql,params SqlParameter[]pms) { using(SqlConnection con=new SqlConnection(conStr)) { using(SqlCommand cmd=new SqlCommand(sql,con)) { if(pms!=null) { cmd.Parameters.AddRange(pms); } con.Open(); return cmd.ExecuteNonQuery(); } } } }
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { List<Area> list = GetDataByParentID(0); LoadDataToTree(treeView1.Nodes,list); } private void LoadDataToTree(TreeNodeCollection treeNodeCollection, List<Area> list) { foreach (Area area in list) { TreeNode node = treeNodeCollection.Add(area.AreaName); //记录当前区域的ID node.Tag = area.AreaID; //设置让TreeView中每个节点单击右键时显示右键菜单 node.ContextMenuStrip = deletecontextMenuStrip1; //获取当前ID对应的所有子节点 List<Area> listChild = GetDataByParentID(area.AreaID); LoadDataToTree(node.Nodes,listChild); } } /// <summary> /// 根据PID获取该区域下面的所有子ID /// </summary> /// <param name="pid"></param> /// <returns></returns> private List<Area> GetDataByParentID(int pid) { List<Area> list=new List<Area>(); string selectCmd = "select * from City where AreaPID=@pid"; using (SqlDataReader reader = SqlHelper.ExecuteReader(selectCmd, new SqlParameter("@pid", pid))) { if (reader.HasRows) { while (reader.Read()) { Area model=new Area(); model.AreaID = reader.GetInt32(0); model.AreaName = reader.GetString(1); model.AreadPID = reader.GetInt32(2); list.Add(model); } } } return list; } private void button2_Click(object sender, EventArgs e) { if (treeView1.SelectedNode != null) { int areaID = (int) treeView1.SelectedNode.Tag; DeleteByAreaID(areaID); treeView1.SelectedNode.Remove(); } } private void DeleteByAreaID(int areaID) { List<Area> list=GetDataByParentID(areaID); foreach(Area item in list) { DeleteByAreaID(item.AreaID); } string sqlCommand="delete from City where AreaID=@id"; SqlParameter pms=new SqlParameter("@id",areaID); SqlHelper.ExecuteNonQuery(sqlCommand,pms); } private void Form1_Load(object sender, EventArgs e) { } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { //获得选中节点 this.treeView1.SelectedNode = e.Node; } private void button2_Click_1(object sender, EventArgs e) { } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { if (treeView1.SelectedNode != null) { int areaID = (int)treeView1.SelectedNode.Tag; DeleteByAreaID(areaID); treeView1.SelectedNode.Remove(); } } }
将XML中的文件节点绑定到TreeView中
<?xml version="1.0" ?> <食物> <水果> <苹果>20元</苹果> </水果> <水果> <橘子>15元</橘子> </水果> </食物>
using System.IO;//引用与输入输出文件流有关的命名空间 using System.Xml;//引用与XML有关的命名空间 namespace BindingXML { public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); } private XmlDocument NexusDocument = new XmlDataDocument();//定义一个XML文档对象 private void BindingXML_Load(object sender, EventArgs e) { string filePath = "NexusPoint.xml";//定义一个变量保存XML文件的路径 if(File.Exists(filePath))//当在指定路径下存在该文件时 { NexusDocument.Load(filePath);//加载该路径下的XML文件 RecursionTreeControl(NexusDocument.DocumentElement,treeView1.Nodes);//将加载完成的XML文件显示在TreeView控件中 treeView1.ExpandAll();//展开TreeView控件中的所有项 } } /// <summary> /// RecursionTreeControl:表示将XML文件的内容显示在TreeView控件中 /// </summary> /// <param name="xmlNode">将要加载的XML文件中的节点元素</param> /// <param name="nodes">将要加载的XML文件中的节点集合</param> private void RecursionTreeControl(XmlNode xmlNode,TreeNodeCollection nodes) { foreach(XmlNode node in xmlNode.ChildNodes)//循环遍历当前元素的子元素集合 { string temp = (node.Value != null ? node.Value : (node.Attributes != null && node.Attributes.Count > 0) ? node.Attributes[0].Value : node.Name);//表示TreeNode节点的文本内容 TreeNode new_child = new TreeNode(temp);//定义一个TreeNode节点对象 nodes.Add(new_child);//向当前TreeNodeCollection集合中添加当前节点 RecursionTreeControl(node,new_child.Nodes);//调用本方法进行递归 } } } }