C# 树状图
效果图:
结构:
frmMain层
using hierarchy.BLL; using hierarchy.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace hierarchy.UI { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } hierarchysBll bll = new hierarchysBll(); List<Thelog> list = null; private void frmMain_Load(object sender, EventArgs e) { tvmo(); } #region MyRegion public void tvmo() { tvMov.Nodes.Clear(); TreeNode dd = null; list = bll.findAll(); foreach (Thelog item in list) { dd = new TreeNode(); if (item.Qfatherid == 0) { dd.Tag = item.Qid; dd.Text = item.Qname; tvMov.Nodes.Add(dd); GetNode(dd, item.Qid); } } } private void GetNode(TreeNode Qname, int Qid) { list = bll.findAll(); TreeNode tn = null; if (list.Count > 0) { foreach (Thelog drv in list) { if (Qid == drv.Qfatherid) { tn = new TreeNode(); tn.Tag = drv.Qid; tn.Text = drv.Qname; Qname.Nodes.Add(tn); GetNode(tn, drv.Qid); } } } } #endregion private void button1_Click(object sender, EventArgs e) { TreeNode node = tvMov.SelectedNode; int s = (int)node.Tag; int i = node.Level; frmaddlei add = new frmaddlei(s, i, node); add.Show(); } private void button3_Click(object sender, EventArgs e) { TreeNode node = tvMov.SelectedNode; int id = (int)node.Tag; bool bl = bll.finddel(id); if (bl) { node.Remove(); MessageBox.Show("删除成功!!"); } else { MessageBox.Show("出错啦!!!"); } } private void button2_Click(object sender, EventArgs e) { TreeNode node = tvMov.SelectedNode; int s = (int)node.Tag; string i = node.Text; frmup add = new frmup(s, i, node); add.Show(); } private void tvMov_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode node = tvMov.SelectedNode; string i = node.Text; MessageBox.Show(i); } } }
删除层
using hierarchy.BLL; using hierarchy.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace hierarchy.UI { public partial class frmaddlei : Form { public frmaddlei() { InitializeComponent(); } int ss = 0; int ii = 0; TreeNode aodee = null; public frmaddlei(int s, int i, TreeNode aode) { ss = s; ii = i; aodee = aode; InitializeComponent(); } hierarchysBll bll = new hierarchysBll(); private void button1_Click(object sender, EventArgs e) { Thelog Thelog = new Thelog(); Thelog.Qname = tbxname.Text; Thelog.Qfatherid=ii; Thelog.Qtier = ss; bool bl = bll.findAdd(Thelog); if (bl) { MessageBox.Show("添加成功"); string guid = Guid.NewGuid().ToString(); TreeNode t = new TreeNode(this.tbxname.Text); t.Tag = guid; aodee.Nodes.Add(t); this.Close(); } } } }
Dal层
using hierarchy.Model; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace hierarchy.DAL { public class hierarchysDAL { //01.写一个查询所有年级的方法 public List<Models> findAllGrades() { List<Models> list = new List<Models>(); string sql = "select [StudentName] from [Address_IS]"; DataTable table = SQLHelper.ExecuteDataTable(sql); foreach (DataRow item in table.Rows) { Models Models = new Models(); Models.StudentName = item["StudentName"].ToString(); list.Add(Models); } return list; } //01.写一个查询所有年级的方法 public List<Thelog> findAll() { List<Thelog> list = new List<Thelog>(); string sql = "SELECT * FROM Thelog"; DataTable table = SQLHelper.ExecuteDataTable(sql); foreach (DataRow item in table.Rows) { Thelog Thelog = new Thelog(); Thelog.Qid = Convert.ToInt32(item["qid"]); Thelog.Qname = item["qname"].ToString(); Thelog.Qtier = Convert.ToInt32(item["qtier"]); Thelog.Qfatherid = Convert.ToInt32(item["qfatherid"]); list.Add(Thelog); } return list; } public bool findAdd(Thelog Thelog) { bool bl = false; string sql = @"INSERT INTO Thelog(qname,qfatherid,qtier) VALUES(@name,@fatherid,@tid)"; SqlParameter[] paras = { new SqlParameter("@name", Thelog.Qname), new SqlParameter("@fatherid", Thelog.Qtier), new SqlParameter("@tid", Thelog.Qfatherid) }; int i = (int)SQLHelper.ExecuteNonQuery(sql,paras); if (i > 0) { bl = true; } return bl; } public bool finddel(int id) { bool bl = false; string sql = "delete from Thelog where qid=@nid"; SqlParameter[] paras = { new SqlParameter("@nid",id) }; int i = (int)SQLHelper.ExecuteNonQuery(sql, paras); if (i > 0) { bl = true; } return bl; } public bool update(Thelog Thelog) { bool bl = false; string sql = "update Thelog set qname=@name where qid=@id"; SqlParameter[] paras = { new SqlParameter("@name", Thelog.Qname), new SqlParameter("@id", Thelog.Qid) }; int i = (int)SQLHelper.ExecuteNonQuery(sql, paras); if (i > 0) { bl = true; } return bl; } } }