Winfrom treeview拖拽功能

设置控件的可拖拽属性为true


实现三个事件

        /*
         当鼠标拖拽某节点时
         */
        private void tvQueuing_ItemDrag(object sender, ItemDragEventArgs e)
        {
            DragWho = 1;
            TreeNode tn = e.Item as TreeNode;
            //当按住鼠标左键拖动节点时移动节点。
            if (e.Button == MouseButtons.Left)
            {
                DoDragDrop(e.Item, DragDropEffects.Move);
            }
            //else if (e.Button == MouseButtons.Right)
            //{
            //    DoDragDrop(e.Item, DragDropEffects.Copy);
            //}
        }

  /*
         当鼠标拖拽节点进入控件区域内时
         */
        private void tvQueuing_DragEnter(object sender, DragEventArgs e)
        {
            //三种都可以
            //if (e.Data.GetDataPresent(typeof(TreeNode)))
            //    e.Effect = DragDropEffects.Move;
            //else
            //    e.Effect = DragDropEffects.None;
            e.Effect = DragDropEffects.Move;
            e.Effect = e.AllowedEffect;
         
        }

真正实现的操作:

     private void tvQueuing_DragDrop(object sender, DragEventArgs e)
        {
            Point Position = new Point(0, 0);
            TreeNode myNode = null;//选中的节点
          
            if (e.Data.GetDataPresent(typeof(TreeNode)))
            {
                myNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
            }
            else
            {
                MessageBox.Show("error");
            }
            Position.X = e.X;
            Position.Y = e.Y;
            Position = tvQueuing.PointToClient(Position);
            TreeNode DropNode = tvQueuing.GetNodeAt(Position);//落下的节点
            //DropNode.BackColor = Color.Red;
            //DropNode.ForeColor = Color.Red;
            // 确定落下的节点不是被拖拽节点本身或者被拖拽节点的子节点
            if (!DropNode.Equals(myNode) && !ContainsNode(DropNode, myNode))
            {
                if (e.Effect == DragDropEffects.Move)
                {
                    DragNodeView(myNode, DropNode, 1);
                }
                else if (e.Effect == DragDropEffects.Copy)
                {
                    myNode.Nodes.Add((TreeNode)DropNode.Clone());
                }
                myNode.Expand();
            }
        
        }



 //确定一个节点是否是父节点。
        private bool ContainsNode(TreeNode node1, TreeNode node2)
        {

            if (node2.Parent == null) return false;
            if (node2.Parent.Equals(node1)) return true;
            return ContainsNode(node1, node2.Parent);
        }

   

        #region  鼠标拖拽效果三个tv公用方法
        /// <summary>
        /// 鼠标拖拽效果三个tv
        /// </summary>
        /// <param name="myNode">鼠标选中的节点</param>
        /// <param name="DropNode">要移动到那个节点下面或者上面的落下节点</param>
        ///  <param name="VehicleStateType_ID">车辆状态类型</param>
        private void DragNodeView(TreeNode myNode, TreeNode DropNode, int VehicleStateType_ID)
        {
            //1找到选中节点的排序
            string sqlmyNode = "SELECT VehicleOrder_ID from EMTinCar where NO='" + myNode.Text + "'";
            string myNodeValue = Common.Conn.SqlHelper.getSqlValue(sqlmyNode);
                int NodeValue=0;
                if (!string.IsNullOrEmpty(myNodeValue))
            {
                NodeValue = Convert.ToInt32(myNodeValue);
            }
            //2找到落下的节点的排序
            string sqlDragNode = "SELECT VehicleOrder_ID from EMTinCar where NO='" + DropNode.Text + "'";
          
            string DragNodeValue = Common.Conn.SqlHelper.getSqlValue(sqlDragNode);
            int DragNodes = 0;
            if (!string.IsNullOrEmpty(DragNodeValue))
            {
                 DragNodes = Convert.ToInt32(DragNodeValue);
            }
            string JuageSql = string.Empty;
            string strupdateSql = string.Empty;
            string strMessage = string.Empty;
            if (NodeValue > DragNodes)
            {
                #region 上移
                //3 小于选中的,大于等于落下的排序加1
                JuageSql = string.Format("exec Up_EMTinCarinfo {0},{1},{2}", VehicleStateType_ID, NodeValue, DragNodes);
                strMessage = Common.Conn.SqlHelper.ExecSql(JuageSql);
                if (strMessage != string.Empty)
                {
                    return;
                }
                #endregion
            }
            else
            {
                #region  下移
                //3把落下的节点的排序,替换之前把之前的排序
                JuageSql = string.Format("exec Up_EMTinCarinfo1 {0},{1},{2}", VehicleStateType_ID, myNodeValue, DragNodeValue);
                strMessage = Common.Conn.SqlHelper.ExecSql(JuageSql);
                if (strMessage != string.Empty)
                {
                    return;
                }
                #endregion
            }

            //4选中节点的排序与落下的节点的排序互换
            DataTable dt = YieldTransitEditBus.getTinByNo(myNode.Text);
            if (dt != null && dt.Rows.Count > 0)
            {
                tin.ID = Convert.ToInt32(dt.Rows[0]["ID"].ToString());
            }
            tin.VehicleOrder_ID = Convert.ToInt32(DragNodeValue);
            tin.VehicleStateType_ID = VehicleStateType_ID;
            YieldTransitEditBus.updateemtin(tin);
       

        }
        #endregion

拖拽操作数据库对应表的字段排序重新

分上移:选中的节点移动到那个节点的范围内小于选中的,大于等于落下的排序加1

--替换之前把之前的排序+1    
CREATE PROC Up_EMTinCarinfo    
 @VehicleStateType_ID int, --车辆状态    
 @VehicleOrder_ID1 int,--选中节点的排序    
 @VehicleOrder_ID2 int --落下的节点的排序    
AS     
declare  up_EMTinCarinfo_cursor  cursor scroll for     
  
  
--结果集    
SELECT ID,VehicleOrder_ID FROM EMTinCar where VehicleStateType_ID =@VehicleStateType_ID  
  
and VehicleOrder_ID >=@VehicleOrder_ID2  AND  VehicleOrder_ID<@VehicleOrder_ID1    
  
  
open up_EMTinCarinfo_cursor    
  
declare @ID int ,@VehicleOrder_ID int    
  
fetch First from up_EMTinCarinfo_cursor into @ID ,@VehicleOrder_ID     
     
WHILE @@fetch_status=0     
  
begin     
  
Update EMTinCar Set VehicleOrder_ID=@VehicleOrder_ID+1 Where ID=@ID     
  
--set @VehicleOrder_ID=@VehicleOrder_ID+1    
fetch next from up_EMTinCarinfo_cursor into @ID ,@VehicleOrder_ID       
     
end    
  
CLOSE up_EMTinCarinfo_cursor    
  
DEALLOCATE up_EMTinCarinfo_cursor    


下移:

--替换之前把之前的排序-1    
CREATE PROC Up_EMTinCarinfo1    
 @VehicleStateType_ID int, --车辆状态    
 @VehicleOrder_ID1 int,--选中节点的排序    
 @VehicleOrder_ID2 int --落下的节点的排序    
AS     
declare  up_EMTinCarinfo_cursor1  cursor scroll for     
----结果集    
SELECT ID,VehicleOrder_ID FROM EMTinCar where VehicleStateType_ID =@VehicleStateType_ID  
  
and VehicleOrder_ID >@VehicleOrder_ID1  AND  VehicleOrder_ID<=@VehicleOrder_ID2   
  
  
  
-- --要循环的结果集  
--SELECT ID ,VehicleStateType_ID from(  
--SELECT ID ,VehicleStateType_ID,isnull(a.ReturnTime,getdate()) ReturnTime  from EMTinCar   
--LEFT JOIN (  
--SELECT max(yt.ID) AS YtID,et.NO,max(ReturnTime) ReturnTime,yt.EMTinCar_ID from YieldTransit yt  
--INNER JOIN EMTinCar et on yt.EMTinCar_ID =et .ID   
--left JOIN YieldTransitReturn on yt.ID=YieldTransitReturn.YieldTransit_ID  
--group BY et.NO ,yt.EMTinCar_ID  
--) a ON EMTinCar_ID=EMTinCar.ID  
--where VehicleStateType_ID=@VehicleStateType_ID) aa order by ReturnTime ASC  
   
   
   
   
open up_EMTinCarinfo_cursor1    
declare @ID int ,@VehicleOrder_ID int    
    
fetch First from up_EMTinCarinfo_cursor1 into @ID ,@VehicleOrder_ID        
WHILE @@fetch_status=0     
begin     
Update EMTinCar Set VehicleOrder_ID=@VehicleOrder_ID-1 Where ID=@ID     
    
fetch next from up_EMTinCarinfo_cursor1 into @ID ,@VehicleOrder_ID          
end    
CLOSE up_EMTinCarinfo_cursor1    
DEALLOCATE up_EMTinCarinfo_cursor1 

            //4选中节点的排序与落下的节点的排序互换
            DataTable dt = YieldTransitEditBus.getTinByNo(myNode.Text);
            if (dt != null && dt.Rows.Count > 0)
            {
                tin.ID = Convert.ToInt32(dt.Rows[0]["ID"].ToString());
            }
            tin.VehicleOrder_ID = Convert.ToInt32(DragNodeValue);
            tin.VehicleStateType_ID = VehicleStateType_ID;
            YieldTransitEditBus.updateemtin(tin);
       

        //修改
        public static bool UPDATEEMTinCarVehicleStateType(EMTinCarModel et)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("exec UPDATE_EMTinCar_VehicleStateType @VehicleStateType_ID ,@VehicleOrder_ID,@ID");
                SqlParameter[] spr = new SqlParameter[]
                {
                      new  SqlParameter ("@VehicleStateType_ID",SqlDbType.Int),
                      new  SqlParameter ("@VehicleOrder_ID",SqlDbType.Int),
                      new  SqlParameter ("@ID",SqlDbType.Int)
                };
                spr[0].Value = et.VehicleStateType_ID;
                spr[1].Value = et.VehicleOrder_ID;
                spr[2].Value = et.ID;
                foreach (SqlParameter  item in spr)
                {
                    if (item .Value ==null)
                    {
                        item.Value = DBNull.Value;
                    }
                }
                int k = Common.Conn.SqlHelper.ExecuteNonQuery(sb.ToString(), spr);
                if (k > 0)
                {
                    return true;
                }
                else {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return  false;
                throw ex;
            }
        }
CREATE proc UPDATE_EMTinCar_VehicleStateType      
   
 @VehicleStateType_ID int,    
  @VehicleOrder_ID int ,        
 @ID int        
as        
UPDATE EMTinCar  set VehicleStateType_ID=@VehicleStateType_ID,VehicleOrder_ID=@VehicleOrder_ID  where ID=@ID




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值