TreeView拖动并存入数据库(可判断拖动)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace treeview
{
    /// <summary>
    /// SelectNodes选择的节点,要更新的
    /// flag判断是否更新成功
    /// employeeName员工表中员工姓名的列号
    /// dept部门中部门名称的列号
    /// </summary>
    public partial class Form1 : Form
    {
        int employeeName = 0 ;
        int dept = 0;
        int employeeDept = 5;
        public static string SelectNodes;

        bool flag = false;

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Form1加载时数据绑定
            dataBindTreeView();
        }
        //返回一个链接池
        public SqlConnection getCon()
        {
            return new SqlConnection("server=.;database=Personnel_Manage;User ID=sa;pwd=123");
        }
        //得到一个包含员工表和部门表的数据集DataSet
        public DataSet getDs()
        {
            SqlConnection con = getCon();
            SqlCommand cmd = new SqlCommand("select * from employee;select * from Dept", con);
            SqlDataAdapter sda = new SqlDataAdapter();           
            sda.SelectCommand = cmd;          
            DataSet ds = new DataSet();
            sda.Fill(ds);
         
            return ds;
         
        

        }

        //绑定TreeView的方法
        public void dataBindTreeView()
        {           
            this.treeView1.Nodes.Clear();
            DataSet dsAll = getDs();
            DataTable Tables1 = dsAll.Tables[0];
            DataTable Tables2 = dsAll.Tables[1];


            for (int i = 0; i < Tables2.Rows.Count; i++)
            {
                TreeNode NodeDept = new TreeNode();
                NodeDept.Text = Tables2.Rows[i][dept].ToString();

                for (int j = 0; j < Tables1.Rows.Count; j++)
                {
                    TreeNode NodeEmployee = new TreeNode();
                    string DeptN = Tables1.Rows[j][employeeDept].ToString().Trim();
                    if (DeptN == NodeDept.Text.Trim())
                    {
                        NodeEmployee.Text = Tables1.Rows[j][employeeName].ToString().Trim();
                        NodeDept.Nodes.Add("", NodeEmployee.Text, 3, 4);
                      
                    }
                }
                treeView1.Nodes.Add(NodeDept);


            }
            
        }

        //把拖放后的变化数据存入数据库
        public bool UpDataDataBase(string nodeDept,string nodeEmployee)
        {
            SqlConnection con = getCon();
            
            SqlCommand cmdu = new SqlCommand("UPDATE employee SET Employeebranch = " + "'" + nodeDept + "'where Employeename = '" + nodeEmployee + "'",con);
            con.Open();
         
            int  count = cmdu.ExecuteNonQuery();

            //成功更新返回true
            if (count > 0)
                return true;
            else
                return false;
        }


        #region
//如果使用者移出视窗外,便会引发DragLeave事件。

//如果滑鼠进入另一个控制项,便会引发该控制项的DragEnter事件。

//如果滑鼠移动,但是待在相同的控制项内,便会引发DragOver事件。


        #endregion

 

        #region //treeView绑定

        //treeView拖动中用于存放“鼠标”上的节点
        private TreeNode MyNode;

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            //启动拖放操作,设置拖放类型为Move(移动)


            TreeView theTreeView = (TreeView)sender;
            TreeNode MoveNode = (TreeNode)e.Item;
            if(MoveNode.Parent == null)
                theTreeView.AllowDrop = false;
            else
            {
                theTreeView.AllowDrop = true;
            }
            //执行拖放方法,参数是要拖放的Node和类型move
          
            DoDragDrop(e.Item, DragDropEffects.Move);
        }


        private void treeView1_DragEnter(object sender, DragEventArgs e)
        {
            // 获取TreeNode节点类型的数据


            TreeNode Objdata = (TreeNode)e.Data.GetData(typeof(TreeNode));

            if (Objdata != null)
            {
                e.Effect = DragDropEffects.Move;


                //改变进入TreeNode的背景色(土黄色)表示你要拖动的节点,
                Objdata.BackColor = Color.FromArgb(195, 90, 0);
                //保存此TreeNode,进入下一个时还原背景色
                MyNode = Objdata;
            }
            else e.Effect = DragDropEffects.None;

        }

        private void treeView1_DragOver(object sender, DragEventArgs e)
        {
            //修改鼠标进入TreeNode的背景色,还原上一个TreeNode的背景色
            TreeView NowTree = (TreeView)sender;
            TreeNode CneterNode = NowTree.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));

            if ((CneterNode != null) && (CneterNode != MyNode))
            {
                //指定颜色,(滑动过节点的背景颜色,)
                MyNode.BackColor = Color.White;
                //知道滑动过节点的字体颜色
                MyNode.ForeColor = Color.Green;
                if (CneterNode.Parent == null)
                {
                    //如果是“部门节点”把颜色换成蓝色
                    CneterNode.BackColor = Color.FromArgb(95, 51, 255);
                    CneterNode.ForeColor = Color.White;   
                }
                    //一般节点为淡绿色
                else
                    CneterNode.BackColor = Color.FromArgb(164, 220, 13);
                MyNode = CneterNode;
            }
        }


        private void treeView1_DragDrop(object sender, DragEventArgs e)
        {
            TreeNode theNode = (TreeNode)e.Data.GetData(typeof(TreeNode));

            SelectNodes = theNode.Text;

            TreeView theTree = (TreeView)sender;
            TreeNode targetNode = theTree.GetNodeAt(treeView1.PointToClient(new Point(e.X, e.Y)));
            targetNode.ForeColor = Color.Green;

            if (targetNode.Parent == null)
            {

                if (targetNode != null)
                {
                    TreeNode targetParent = targetNode.Parent;
                    //删除拖放的TreeNode
                    theNode.Remove();
                    //添加到目标TreeNode下
                    targetNode.Nodes.Add(theNode);
                    targetNode.BackColor = Color.White;
                    theTree.SelectedNode = targetNode;

                }
                string parentsText = targetNode.Text;
                if (UpDataDataBase(parentsText, SelectNodes))
                {
                   flag = true;               
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    SelectNodes = "";
                  
                }

                else
                {
                    MessageBox.Show("请重新选择选择员工", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dataBindTreeView();
                }

            }

            else
            {
                MessageBox.Show("请选择部门", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
           
            }

            if (!flag)
            {

                dataBindTreeView();
               
            }

        }

 

    }
}

 

上一篇:bootstrap-treeview


下一篇:javascript – 具有本地数组数据的jQuery花式树