the using the System; the using the System.Collections.Generic; the using the System.ComponentModel; the using the System.Data; the using the System.Drawing; the using the System.Linq; the using the System.Text; the using the System.Windows.Forms; the using the System.Data.SqlClient; namespace the TreeView { /// <the Summary> /// node SelectNodes selected to be updated /// Flag determine whether the update was successful /// employeeName employee table employee name column number /// column number dept department in the name of the department /// </ Summary> public partial classForm1: Form1 { int the employeeName = 0 ; int Dept = 0 ; int employeeDept = . 5 ; public static String the SelectNodes; BOOL In Flag = to false ; public form1 () { the InitializeComponent (); } Private void the Form1_Load ( Object SENDER, EventArgs E) { // when loading data binding Form1 dataBindTreeView (); } // returns a connection pooling public the SqlConnection getCon () { return new new the SqlConnection ( " Server Database .; = = Personnel_Manage; the User ID = SA; pwd = 123 " ); } // get employee table and a department table containing data sets the DataSet public the DataSet getDs () { the SqlConnection CON = getCon (); the SqlCommand cmd = new new the SqlCommand ( " SELECT * from Employee; SELECT * from Dept's " , CON); the SqlDataAdapter SDA = new new the 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] [the employeeName] .ToString () Trim (); NodeDept.Nodes.Add ( "" , NodeEmployee.Text, . 3 , . 4 ); } } TreeView1.Nodes.Add (NodeDept); } } / / the change of the data stored in the database and drop public BOOL UpDataDataBase ( String nodeDept, String nodeEmployee) { the SqlConnection CON = getCon (); the SqlCommand cmdu = new new the SqlCommand ( "Employee Employeebranch the SET = the UPDATE " + " ' " + nodeDept + " ' WHERE EmployeeName = ' " + nodeEmployee + " ' " , CON); con.Open (); int COUNT = cmdu.ExecuteNonQuery (); // updated successfully returned to true IF (COUNT> 0 ) return to true ; the else return to false ; } #region // If the user out of the window, the events will trigger DragLeave. @ If the mouse enters another control key, will trigger the control term DragEnter event. //If you move the mouse, but stay within the same controls, it will lead to DragOver event. #endregion #region // the treeView binding // the treeView nodes for storing the drag "mouse" Private the TreeNode MyNode; Private void treeView1_ItemDrag ( Object SENDER, ItemDragEventArgs E) { // Start drop operation, drag and drop type is provided is a move (moving) the TreeView theTreeView = (the TreeView) SENDER; the TreeNode MoveNode = (the TreeNode) e.Item; IF (MoveNode.Parent == null ) theTreeView.AllowDrop = to false ; the else { TheTreeView.AllowDrop = to true ; } // drag and drop method, to drag and drop the parameter type and Node Move the DoDragDrop (e.Item, DragDropEffects.Move); } Private void treeView1_DragEnter ( Object SENDER, the DragEventArgs E) { // Get the node type of data TreeNode TreeNode Objdata = (TreeNode) e.Data.GetData ( typeof (TreeNode)); IF (! Objdata = null ) { e.Effect = DragDropEffects.Move; //Enter TreeNode change the background color (khaki) that you want to drag a node, Objdata.BackColor = Color.FromArgb ( 195 , 90 , 0 ); // save the TreeNode, the next time reduction enter a background color MyNode = Objdata; } the else e.Effect = DragDropEffects.None; } Private void treeView1_DragOver ( Object SENDER, the DragEventArgs E) { // modify the background color of the mouse entering the TreeNode, a reduction of the background color TreeNode the TreeView NowTree = (the TreeView) SENDER; TreeNode CneterNode = NowTree.GetNodeAt (treeView1.PointToClient (new new Point (eX, eY))); IF ! ((CneterNode = null !) && (CneterNode = MyNode)) { // the specified color (background color slide over node,) MyNode.BackColor = Color.White; / / know sliding nodal font color MyNode.ForeColor = Color.green; IF (CneterNode.Parent == null ) { // If "department node" the color into blue CneterNode.BackColor = Color.FromArgb ( 95 , 51 is , 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 ( " Please choose Select employees " , " tips " , MessageBoxButtons.OK, MessageBoxIcon.Error); dataBindTreeView (); } } the else { MessageBox.Show ( " Please select the department " , " tips " , MessageBoxButtons.OK , MessageBoxIcon.Warning); } IF (! In Flag) { dataBindTreeView (); } } } }