DevExpress SpreasheetControl 单元格拖拽

1、设置spreadsheetcontrol AllowDrag 为true

2、

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Spreadsheet;
using DevExpress.Services;
using DevExpress.XtraSpreadsheet;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            IMouseHandlerService oldMouseHandler = (IMouseHandlerService)spreadsheetControl1.GetService(typeof(IMouseHandlerService));
            if(oldMouseHandler != null) {
                MyMouseHandlerService newMouseHandler = new MyMouseHandlerService(spreadsheetControl1, oldMouseHandler);
                spreadsheetControl1.RemoveService(typeof(IMouseHandlerService));
                spreadsheetControl1.AddService(typeof(IMouseHandlerService), newMouseHandler);
            }
        }

        private void spreadsheetControl1_DragDrop(object sender, DragEventArgs e) {
            Cell SpreadsheetCell = spreadsheetControl1.GetCellFromPoint(spreadsheetControl1.PointToClient(new Point(e.X, e.Y)));

            if(SpreadsheetCell != null) {
                SpreadsheetCell.Worksheet.Selection.Clear();
                SpreadsheetCell.Worksheet.SelectedCell = SpreadsheetCell;
            }
            spreadsheetControl1.Refresh();
        }

        private void spreadsheetControl1_DragEnter(object sender, DragEventArgs e) {
            Cell SpreadsheetCell = spreadsheetControl1.GetCellFromPoint(spreadsheetControl1.PointToClient(new Point(e.X, e.Y)));
            e.Effect = (SpreadsheetCell != null ? DragDropEffects.Move : DragDropEffects.None);
        }

        private void spreadsheetControl1_DragOver(object sender, DragEventArgs e) {
            Cell SpreadsheetCell = spreadsheetControl1.GetCellFromPoint(spreadsheetControl1.PointToClient(new Point(e.X, e.Y)));
            if (SpreadsheetCell.GetReferenceA1() == "B2")
                e.Effect = DragDropEffects.None;
            else
                e.Effect = (SpreadsheetCell != null ? DragDropEffects.Move : DragDropEffects.None);
        }
    }

    public class MyMouseHandlerService : MouseHandlerServiceWrapper {
        IServiceProvider provider;
        Point StartCoords = Point.Empty;

        public MyMouseHandlerService(IServiceProvider provider, IMouseHandlerService service)
            : base(service) {
            this.provider = provider;
        }

        public override void OnMouseDown(MouseEventArgs e) {
            if(e.Button == System.Windows.Forms.MouseButtons.Left) StartCoords = e.Location;                
            base.OnMouseDown(e);
        }

        public override void OnMouseUp(MouseEventArgs e) {
            StartCoords = Point.Empty;
            base.OnMouseUp(e);
        }

        public override void OnMouseMove(MouseEventArgs e) {
            if(e.Button == System.Windows.Forms.MouseButtons.Left && StartCoords != Point.Empty) {
                Cell SpreadsheetCell = (provider as SpreadsheetControl).GetCellFromPoint(StartCoords);

                if(System.Math.Abs(StartCoords.X - e.X) > 10 || System.Math.Abs(StartCoords.Y - e.Y) > 10) {
                    (provider as SpreadsheetControl).DoDragDrop(SpreadsheetCell, DragDropEffects.Move);
                    base.OnMouseUp(e);
                }
            }
            else
                base.OnMouseMove(e);
        }
    }
}

你可能感兴趣的:(DevExpress SpreasheetControl 单元格拖拽)