在DevExpress的GridControl中增加拖拽行记录排序

孔子说,“人的毛病都是惯出来的”,古人说的对。

现在用户对按钮点击排序不满意了, 希望拖拽排序,,好吧,身为码农就是苦B的命。

重载GridControl控件,对MouseMove、MouseDown、MouseUp事件修改一下。

多谢涛神提供的获取行信息,据说这是内部传阅的资资料 。

运行效果:

在DevExpress的GridControl中增加拖拽行记录排序_第1张图片

 

代码:

 public class Gc11 : DevExpress.XtraGrid.GridControl
    {

        GridView gv22 = null;
        Point m_mouseDownLocation;
        int m_dragHandle;
        DragForm m_dragRowShadow;

        /// 
        /// 
        /// 
        /// 
        protected override void OnMouseDown(MouseEventArgs ev)
        {
            if (ev.Button == MouseButtons.Left)
            {
                //只允许鼠标左键拖拽功能
                if (gv22 == null)
                {
                    gv22 = ((GridView)base.MainView);
                }
                //看看点击到哪个地方,如果点击了记录行,允许拖拽,只是允许,还没有开始拖拽。
                var _hit = gv22.CalcHitInfo(ev.Location);
                if (_hit.RowHandle >= 0)
                {
                    m_dragHandle = _hit.RowHandle;
                    m_mouseDownLocation = ev.Location;
                }
                else
                {
                    m_dragHandle = -1;
                }
            }

            base.OnMouseDown(ev);

        }

        /// 
        /// 
        /// 
        /// 
        protected override void OnMouseMove(MouseEventArgs ev)
        {
            if (ev.Button == MouseButtons.Left && m_dragHandle >= 0)
            {
                if (m_dragRowShadow == null)
                {
                    double _x2 = Math.Pow((ev.Location.X - m_mouseDownLocation.X), 2);
                    double _y2 = Math.Pow((ev.Location.Y - m_mouseDownLocation.Y), 2);
                    double _d2 = Math.Sqrt(_x2 + _y2);
                    if (_d2 > 3)
                    {
                        //鼠标移动的距离,可以判定不是手抖,启动拖拽功能。
                        //执行拖拽;
                        this.BeginDrag(m_dragHandle);
                        //var _info = (DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)gv22.GetViewInfo();
                        //_info.GetGridRowInfo(0).CalcRectangle 
                    }
                }
                else
                {
                    m_dragRowShadow.Location = new Point(m_dragRowShadow.Location.X, this.PointToScreen(ev.Location).Y);
                }

            }
            base.OnMouseMove(ev);
        }

        /// 
        /// 
        /// 
        /// 
        protected override void OnMouseUp(MouseEventArgs ev)
        {

            if (m_dragRowShadow != null)
            {
                var _hit = gv22.CalcHitInfo(ev.Location);
                this.EndDrag(_hit.RowHandle);
            }

            base.OnMouseUp(ev);
        }

        private void BeginDrag(int _handle)
        {
            var _info = (DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)gv22.GetViewInfo();
            //_info.GetGridRowInfo(0).CalcRectangle 

            Rectangle _bound = _info.GetGridRowInfo(_handle).Bounds;
            _bound.Location = this.PointToScreen(_bound.Location);
            m_dragRowShadow = new NetTest1.DragForm(_bound);
            m_dragRowShadow.Show();
        }

        private void EndDrag(int _handle)
        {
            if (m_dragRowShadow != null)
            {
                m_dragRowShadow.Close();
                m_dragRowShadow.Dispose();
                m_dragRowShadow = null;

                int _rowIndex = gv22.GetDataSourceRowIndex(m_dragHandle);

                DataRow _row = ((DataTable)this.DataSource).Rows[_rowIndex];
                object[] _values = _row.ItemArray;

                base.BeginUpdate();

                //移除目标行;
                ((DataTable)this.DataSource).Rows.RemoveAt(m_dragHandle);
                _row = ((DataTable)this.DataSource).NewRow();
                _row.ItemArray = _values;
                if (_handle >= 0)
                {
                    //插入指定位置;
                    ((DataTable)this.DataSource).Rows.InsertAt(_row, _handle);
                    gv22.FocusedRowHandle = _handle;
                }
                else
                {
                    //添加;
                    ((DataTable)this.DataSource).Rows.Add(_row);
                    gv22.FocusedRowHandle = gv22.RowCount - 1;
                }

                base.EndUpdate();



            }
        }


    }

完整代码如下:

链接:https://pan.baidu.com/s/1GjSbGUpdL2kDxVO5FTXsow
提取码:ew3n
 

 

你可能感兴趣的:(.NET)