DevExpress GridControl Drag Drop

DevExpress控件拖拽大多数围绕CalcHitInfo( Point )方法,所以重点在于point坐标是原值还是PointToClient之后的

之前按照TreeList拖拽改出一个Demo,但发现GridControl在winform中拖拽可行,但放在dockpanel里就无法得到目标行的正确RowHandle。

经咨询客服,得关键代码如下:

        GridHitInfo hitInfo = null;
        private void gridControl1_MouseDown(object sender, MouseEventArgs e)
        {
            hitInfo = gridView.CalcHitInfo(new Point(e.X, e.Y));

        }

        private void gridControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (hitInfo == null) return;
            if (e.Button != MouseButtons.Left) return;
            Rectangle dragRect = new Rectangle(new Point(
                hitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2,
                hitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2), SystemInformation.DragSize);
            if (!(hitInfo.RowHandle == GridControl.InvalidRowHandle) && !dragRect.Contains(new Point(e.X, e.Y)))
            {
                Object data = gridView.GetRow(hitInfo.RowHandle);
                gridControl.DoDragDrop(data, DragDropEffects.Move);
            }

        }

        private void gridControl_DragDrop(object sender, DragEventArgs e)
        {
            GridControl grid = sender as GridControl;
            Point pt = grid.PointToClient(new Point(e.X, e.Y));
            GridView view = grid.GetViewAt(pt) as GridView;
            if(view == null)
                return;
            GridHitInfo hitInfo = gridView.CalcHitInfo(pt);
            if (hitInfo.InDataRow )
                MessageBox.Show(hitInfo.RowHandle.ToString()+"----"+this.hitInfo.RowHandle.ToString());            
        }


        private void gridControl_DragOver(object sender, DragEventArgs e)
        {
            GridControl grid = sender as GridControl;
            Point pt = grid.PointToClient(new Point(e.X, e.Y));
            GridView view = grid.GetViewAt(pt) as GridView;
            if(view == null)
                return;
            GridHitInfo hitInfo = gridView.CalcHitInfo(pt);
            if(hitInfo.InDataRow)
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }
Demo下载



你可能感兴趣的:(DevExpress GridControl Drag Drop)