/******************************
* 说明:表格拖拽到表格
* 创建人:龚宇超
* 创建日期: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.XtraGrid;
using Lskj.Util;
using DevExpress.XtraGrid.Views.BandedGrid;
namespace Lskj.Control.Model
{
///
/// 表格拖拽到表格
///
public class BandedGridDragGrid
{
///
/// 是否正在拖拽
///
private bool _draging = false;
///
/// 拖动位置
///
private GridHitInfo _hitInfo;
///
/// 当前操作的GridView
///
private BandedGridView _sourceGridView;
///
/// 目标GridView
///
private GridView _targetGridView;
///
/// 拖动完成后调用
///
public event BandedGridFragGridCompleteEventHandler OnDragComplete;
///
/// 顺序值的集合
///
private List DragHander = new List();
public bool isDropColorVisble = false;
///
/// 外部条件是否允许拖拽
///
public bool isDragCond = true;
///
/// Initializes a new instance of the class.
///
/// 当前GridView
/// 目标GridView.
public BandedGridDragGrid(BandedGridView sourceView, GridView targetView)
{
this._sourceGridView = sourceView;
this._targetGridView = targetView;
//记录所有拖拽类到来源表中,后每次进入目标表禁用其他拖拽类的拖拽事件
if (StaticBandedControl.BandedGridViewDragGridDic.ContainsKey(sourceView))
{
List BandedGridDragGrids = StaticBandedControl.BandedGridViewDragGridDic[sourceView];
if (!BandedGridDragGrids.Contains(this))
{
BandedGridDragGrids.Add(this);
}
}
else
{
List BandedGridDragGrids = new List();
BandedGridDragGrids.Add(this);
StaticBandedControl.BandedGridViewDragGridDic.Add(sourceView, BandedGridDragGrids);
}
if (StaticBandedControl.BandedTargetViewDragGridDic.ContainsKey(targetView))
{
List BandedGridDragGrids = StaticBandedControl.BandedTargetViewDragGridDic[targetView];
if (!BandedGridDragGrids.Contains(this))
{
BandedGridDragGrids.Add(this);
}
}
else
{
List BandedGridDragGrids = new List();
BandedGridDragGrids.Add(this);
StaticBandedControl.BandedTargetViewDragGridDic.Add(targetView, BandedGridDragGrids);
}
if (this._sourceGridView == null || this._targetGridView == 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._sourceGridView.RowClick += OnRowClick;
this._targetGridView.GridControl.AllowDrop = true;
this._targetGridView.GridControl.DragOver += new DragEventHandler(gridControl_DragOver);
this._targetGridView.GridControl.DragEnter += new System.Windows.Forms.DragEventHandler(gridControl_DragEnter);
this._targetGridView.GridControl.DragDrop += new System.Windows.Forms.DragEventHandler(gridControl_DragDrop);
this._targetGridView.GridControl.DragLeave += new EventHandler(gridControl_DragLeave);
}
///
/// 点击时候判断位置
///
///
///
private void OnRowClick(object sender, RowClickEventArgs e)
{
int[] dragRowsIndex = _sourceGridView.GetSelectedRows();
int newGetSelect = _sourceGridView.FocusedRowHandle;
if (dragRowsIndex.Length > 1)
{
if (DragHander.Count > dragRowsIndex.Length)
{
DragHander.Remove(newGetSelect);
}
else
{
if (dragRowsIndex.Length == 2)
{
int firstIndex = dragRowsIndex.Cast().FirstOrDefault(x => x != newGetSelect);
if (!DragHander.Contains(firstIndex))
{
DragHander.Add(firstIndex);
}
DragHander.Add(newGetSelect);
}
else
{
if (!DragHander.Contains(newGetSelect))
{
DragHander.Add(newGetSelect);
}
}
}
}
else
{
DragHander.Clear();
}
}
///
/// 说明:获取表格选中行数据
/// 创建人:龚宇超
/// 创建日期: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._sourceGridView.GridControl.AllowDrop = false;
this._targetGridView.GridControl.AllowDrop = true;
this._hitInfo = null;
if (System.Windows.Forms.Control.ModifierKeys != Keys.None) return;
BandedGridView gridView = sender as BandedGridView;
GridHitInfo hitInfo = gridView.CalcHitInfo(new Point(e.X, e.Y));
if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0)
{
this._draging = true;
StaticBandedControl.SourceDragBandedGridView = gridView;
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)
{
BandedGridView gridView = sender as BandedGridView;
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 >= 0 ? this._hitInfo.RowHandle : 0);
if (row != null)
{
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)
{
StaticBandedControl.SourceDragBandedGridView = null;
this._draging = false;
}
///
/// 说明:移动到目标表格上
/// 创建人:龚宇超
/// 创建日期:2017-11-13
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
private void gridControl_DragOver(object sender, DragEventArgs e)
{
GridControl gridControl = (GridControl)sender;
GridView gridView = gridControl.MainView as GridView;
this._draging = true;
if (StaticBandedControl.SourceDragBandedGridView != null && StaticBandedControl.BandedGridViewDragGridDic.ContainsKey(StaticBandedControl.SourceDragBandedGridView))
{
List BandedGridDragGrids = StaticBandedControl.BandedGridViewDragGridDic[StaticBandedControl.SourceDragBandedGridView];
foreach (BandedGridDragGrid item in BandedGridDragGrids)
{
if (item != null && item._targetGridView == gridView)
{
item._draging = true;
}
else
{
item._draging = false;
}
}
}
if (StaticBandedControl.BandedTargetViewDragGridDic.ContainsKey(gridView))
{
List BandedGridDragGrids = StaticBandedControl.BandedTargetViewDragGridDic[gridView];
foreach (BandedGridDragGrid item in BandedGridDragGrids)
{
if (item != null && StaticBandedControl.SourceDragBandedGridView != null && item._sourceGridView == StaticBandedControl.SourceDragBandedGridView)
{
item._draging = true;
}
else
{
item._draging = false;
}
}
}
GridControl targetControl = (sender as GridControl);
GridView targetGrid = targetControl.FocusedView as GridView;
Point gcPoint = targetControl.PointToScreen((sender as GridControl).Location);
Point pt = new Point(e.X - gcPoint.X, e.Y - gcPoint.Y);
_hitInfo = targetGrid.CalcHitInfo(pt);
if (_hitInfo == null || _hitInfo.RowHandle < 0) return;
if (!isDropColorVisble) targetGrid.SelectRow(_hitInfo.RowHandle);
targetGrid.FocusedRowHandle = _hitInfo.RowHandle;
}
///
/// 说明:离开目标表格
/// 创建人:龚宇超
/// 创建日期:2017-11-13
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
private void gridControl_DragLeave(object sender, EventArgs e)
{
this._draging = false;
}
///
/// 说明:移动到目标表格
/// 创建人:龚宇超
/// 创建日期:2017-11-13
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
protected void gridControl_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (isDragCond)
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
///
/// 说明:拖动完成时
/// 创建人:龚宇超
/// 创建日期:2017-11-13
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
protected void gridControl_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if (this.OnDragComplete != null && this._draging)
{
this._draging = false;
this.isDragCond = true;
BandedGridDragGridArgs args = new BandedGridDragGridArgs();
args.SourceGridViewObj = this._sourceGridView;
args.TargetGridViewObj = this._targetGridView;
args.GridViewSelectRows = GetGridSelectRows(DragHander.Count > 1 && this._sourceGridView.GetSelectedRows().Length == DragHander.Count ? DragHander.ToArray() : this._sourceGridView.GetSelectedRows());
args.SelectGridRow = this._targetGridView.GetDataRow(this._targetGridView.FocusedRowHandle);
this.OnDragComplete(sender, args);
this._draging = false;
}
}
#region 表格第一列添加复选框
///
/// 说明:表格第一列添加复选框
/// 创建人:唐德馨
/// 创建日期:2021-07-08
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The grid columnFilterPopupChecke.
public static void GridAddCheckBox(GridView gridview)
{
gridview.OptionsSelection.MultiSelect = true;
gridview.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;
gridview.OptionsSelection.EnableAppearanceFocusedCell = false;
gridview.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDown;
foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridview.Columns)
{
GridColumnModel columnModel = (GridColumnModel)column.Tag;
if (gridview.Columns.IndexOf(column) > 0 && columnModel != null && columnModel.CanSum)
{
foreach (GridSummaryItem gridSummaryItem in column.Summary)
{
gridSummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom;
}
GridSummaryItem gridGroupSummaryItem = gridview.GroupSummary.Where(n => n.FieldName.Equals(column.FieldName)).FirstOrDefault();
if (gridGroupSummaryItem != null)
{
gridGroupSummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom;
}
}
}
}
#endregion
}
}