winform中拖动功能实现技巧

实现的需求,我通过拖动选中的用户行放到左边的机构节点上,从而实现用户改变组织机构的关系

贴代码

 private DataGridViewSelectedRowCollection sourceRowCollection = null;
        private int rowIndexFromMouseDown;

        /// <summary>
        /// 用户拖动到机构里改变所属机构关系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvUser_MouseDown(object sender, MouseEventArgs e)
        {
            rowIndexFromMouseDown = dgvUser.HitTest(e.X, e.Y).RowIndex;
            if (rowIndexFromMouseDown != -1)
            {
                if (this.dgvUser.SelectedRows.Count > 0)
                {
                    sourceRowCollection = this.dgvUser.SelectedRows;
                }
            }
            else
            {
                sourceRowCollection = null;
            }
        }

        private void dgvUser_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (sourceRowCollection != null)
                {
                    DragDropEffects effect = this.dgvUser.DoDragDrop(sourceRowCollection, DragDropEffects.Move);
                    if (effect == DragDropEffects.Move)
                    {
                        //在sourceGrid中移除选中行
                        foreach (DataGridViewRow row in sourceRowCollection)
                        {
                            this.dgvUser.Rows.Remove(row);
                        }
                        sourceRowCollection = null;
                    }
                }
            }
        }

        private void tvwOrganize_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
                {
                    DataGridViewSelectedRowCollection rowCollection = e.Data.GetData(typeof(DataGridViewSelectedRowCollection)) as DataGridViewSelectedRowCollection;
                    if (rowCollection == null)
                    {
                        return;
                    }
                    string userid = (rowCollection[0].DataBoundItem as DataRowView).Row["UserID"].ToString();
                    string organizeid = (rowCollection[0].DataBoundItem as DataRowView).Row["OrganizeID"].ToString();
                    Point p = this.tvwOrganize.PointToClient(new Point(e.X, e.Y));
                    TreeViewHitTestInfo hitTest = tvwOrganize.HitTest(p.X, p.Y);
                    Organize organize = hitTest.Node.Tag as Organize;
                    if (organizeid != organize.OrganizeID && userBiz.UpdateUserOrganize(userid, organize.OrganizeID))
                    {
                        e.Effect = DragDropEffects.Move;
                    }
                    else
                    { e.Effect = DragDropEffects.None; }
                }
            }
            catch (Exception ex)
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void tvwOrganize_DragOver(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
            {
                e.Effect = DragDropEffects.None;
                return;
            }
            else
            {
                e.Effect = DragDropEffects.Move;  //这个值会返回给DoDragDrop方法
            }
        }

 

转载于:https://www.cnblogs.com/njcxwz/p/4602500.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值