DevExpress GridControl gridview 拖动(拖拽)行排序,并且保存排序后的顺序

视频讲解地址 https://www.bilibili.com/video/BV11Y411u7Gb

先看一下效果图

源码下载地址: https://pan.baidu.com/s/1A0GhycAIQPOEveBma9o8dg?pwd=d156 提取码: d156

在这里我们是继承GridControl,重写基类的功能核心代码如下

using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// 代码出自 www.luofenming.com
    /// </summary>
    public class MyGridControl : DevExpress.XtraGrid.GridControl
    {  
        GridView gv =null;
        Point m_mouseDownLocation;
        int m_dragHandle;
        DragForm m_dragRowShadow;
        
        public MyGridControl()
        {
            this.Load += MyGridControl_Load;
        }
        private void MyGridControl_Load(object sender, EventArgs e)
        {
            if (gv == null)
            {
                gv = ((GridView)base.MainView);
                gv.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator;
                gv.IndicatorWidth = 45;
            }
        }
        protected override void OnMouseDown(MouseEventArgs ev)
        {
            if (ev.Button == MouseButtons.Left)
            {
                var _hit = gv.CalcHitInfo(ev.Location);
                if (_hit.RowHandle >= 0)
                {
                    m_dragHandle = _hit.RowHandle;
                    m_mouseDownLocation = ev.Location;
                }
                else
                {
                    m_dragHandle = -1;
                }
            }
            base.OnMouseDown(ev);
        }
        private void gridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle > -1)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
        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);
                    }
                }
                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 = gv.CalcHitInfo(ev.Location);
                this.EndDrag(_hit.RowHandle);
            }
            base.OnMouseUp(ev);
        }

        private void BeginDrag(int _handle)
        {
            var _info = (DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)gv.GetViewInfo();

            Rectangle _bound = _info.GetGridRowInfo(_handle).Bounds;
            _bound.Location = this.PointToScreen(_bound.Location);
            m_dragRowShadow = new 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 = gv.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);
                    gv.FocusedRowHandle = _handle;
                }
                else
                {
                    //添加;
                    ((DataTable)this.DataSource).Rows.Add(_row);
                    gv.FocusedRowHandle = gv.RowCount - 1;
                }
                //DataTable dt = this.DataSource as DataTable;
                //JosnConfigHelper.WriteConfig(dt);//修改本地数据
                base.EndUpdate();
            }
        }

    }
}
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// 代码出自 www.luofenming.com
    /// </summary>
    public class DragForm : DevExpress.Utils.Win.TopFormBase
    {
        Bitmap m_buff;
        Graphics m_buffG;

        public DragForm(Rectangle _bound)
        {
            this.Text = "";
            this.FormBorderStyle = FormBorderStyle.None;
            this.ControlBox = false;
            this.Size = _bound.Size;
            this.ShowInTaskbar = false;
            this.StartPosition = FormStartPosition.Manual;

            this.Opacity = 0.8d; //TopFormBase已经有默认值了;

            m_buff = new Bitmap(_bound.Width, _bound.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            m_buffG = Graphics.FromImage(m_buff);
            m_buffG.CopyFromScreen(_bound.Location, new Point(0, 0), _bound.Size);

            this.BackgroundImageLayout = ImageLayout.None;
            this.BackgroundImage = m_buff;

            this.Location = _bound.Location;

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            // e.Graphics.DrawImage(m_buff, 0, 0);
        }
    }
}