/****************************** * 说明:表格拖拽到表格 * 创建人:龚宇超 * 创建日期:2017-11-13 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using System.Drawing; using System.Windows.Forms; using DevExpress.Utils; using System.Data; using DevExpress.XtraEditors; namespace Lskj.Control.Model { /// /// 表格拖拽到MomeEdit /// public class GridDragMemo { /// /// 是否正在拖拽 /// private bool _draging = false; /// /// 拖动位置 /// private GridHitInfo _hitInfo; /// /// 当前操作的GridView /// private GridView _sourceGridView; /// /// 目标GridView /// private MemoEdit _targetMemoEdit; /// /// 拖动完成后调用 /// public event GridFragMemoEditCompleteEventHandler OnDragComplete; /// /// Initializes a new instance of the class. /// /// 当前GridView /// 目标GridView. public GridDragMemo(GridView sourceView, MemoEdit targetMemoEdit) { this._sourceGridView = sourceView; this._targetMemoEdit = targetMemoEdit; if (this._sourceGridView == null || this._targetMemoEdit == null) return; this._sourceGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(sourceGridView_MouseDown); this._sourceGridView.MouseMove += new System.Windows.Forms.MouseEventHandler(sourceGridView_MouseMove); this._sourceGridView.MouseUp += new System.Windows.Forms.MouseEventHandler(sourceGridView_MouseUp); this._targetMemoEdit.AllowDrop = true; this._targetMemoEdit.DragEnter += new System.Windows.Forms.DragEventHandler(memoEdit_DragEnter); this._targetMemoEdit.DragDrop += new System.Windows.Forms.DragEventHandler(memoEdit_DragDrop); } /// /// 说明:获取表格选中行数据 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The row handles. /// DataRow[]. private DataRow[] GetGridSelectRows(int[] rowHandles) { List rows = new List(); foreach (int handle in rowHandles) { DataRow row = this._sourceGridView.GetDataRow(handle); rows.Add(row); } return rows.ToArray(); } /// /// 说明:源表格鼠标按下 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void sourceGridView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { this._hitInfo = null; if (System.Windows.Forms.Control.ModifierKeys != Keys.None) return; GridView gridView = sender as GridView; GridHitInfo hitInfo = gridView.CalcHitInfo(new Point(e.X, e.Y)); if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0) { this._draging = true; this._hitInfo = hitInfo; } } /// /// 说明:源表格鼠标移动 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void sourceGridView_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { GridView gridView = sender as GridView; if (e.Button == MouseButtons.Left && this._hitInfo != null) { Size dragSize = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(new Point(this._hitInfo.HitPoint.X - dragSize.Width / 2, this._hitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if (!dragRect.Contains(new Point(e.X, e.Y))) { object row = gridView.GetRow(this._hitInfo.RowHandle); gridView.GridControl.DoDragDrop(row, DragDropEffects.Move); this._hitInfo = null; DXMouseEventArgs.GetMouseArgs(e).Handled = true; } } } /// /// 说明:源表格鼠标放开 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void sourceGridView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this._draging = false; } /// /// 说明:移动到目标表格 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void memoEdit_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { e.Effect = DragDropEffects.Move; } /// /// 说明:拖动完成时 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void memoEdit_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { if (this.OnDragComplete != null && this._draging) { this._draging = false; GridDragMemoArgs args = new GridDragMemoArgs(); args.SourceGridViewObj = this._sourceGridView; args.TargetMemoEditObj = this._targetMemoEdit; args.GridViewSelectRows = GetGridSelectRows(this._sourceGridView.GetSelectedRows()); this.OnDragComplete(sender, args); } } } }