e6ec3e4886
SVN-Revision: r924
536 lines
23 KiB
C#
536 lines
23 KiB
C#
/******************************
|
|
* 说明:表格拖拽到表格
|
|
* 创建人:龚宇超
|
|
* 创建日期: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.XtraTreeList.Nodes;
|
|
using DevExpress.XtraTreeList;
|
|
using DevExpress.XtraTreeList.Columns;
|
|
using DevExpress.Data;
|
|
|
|
namespace Lskj.Control.Model
|
|
{
|
|
/// <summary>
|
|
/// 表格拖拽到表格
|
|
/// </summary>
|
|
public class GridDragGrid
|
|
{
|
|
/// <summary>
|
|
/// 是否正在拖拽
|
|
/// </summary>
|
|
private bool _draging = false;
|
|
/// <summary>
|
|
/// 拖动位置
|
|
/// </summary>
|
|
private GridHitInfo _hitInfo;
|
|
/// <summary>
|
|
/// 当前操作的GridView
|
|
/// </summary>
|
|
private GridView _sourceGridView;
|
|
/// <summary>
|
|
/// 目标GridView
|
|
/// </summary>
|
|
private GridView _targetGridView;
|
|
/// <summary>
|
|
/// 拖动完成后调用
|
|
/// </summary>
|
|
public event GridFragGridCompleteEventHandler OnDragComplete;
|
|
/// <summary>
|
|
/// 顺序值的集合
|
|
/// </summary>
|
|
private List<int> DragHander = new List<int>();
|
|
public bool isDropColorVisble = false;
|
|
/// <summary>
|
|
/// 外部条件是否允许拖拽
|
|
/// </summary>
|
|
public bool isAllowDrag = true;
|
|
/// <summary>
|
|
/// 拖动条件
|
|
/// </summary>
|
|
public string DragConditions;
|
|
/// <summary>
|
|
/// 处理目标表格多选
|
|
/// </summary>
|
|
public bool TargetTableMultiSelect=true;
|
|
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="GridDragGrid"/> class.
|
|
/// </summary>
|
|
/// <param name="curGridView">当前GridView</param>
|
|
/// <param name="tarGridView">目标GridView.</param>
|
|
public GridDragGrid(GridView sourceView, GridView targetView)
|
|
{
|
|
this._sourceGridView = sourceView;
|
|
this._targetGridView = targetView;
|
|
//记录所有拖拽类到来源表中,后每次进入目标表禁用其他拖拽类的拖拽事件
|
|
if (StaticControl.GridViewDragGridDic.ContainsKey(sourceView))
|
|
{
|
|
List<GridDragGrid> gridDragGrids = StaticControl.GridViewDragGridDic[sourceView];
|
|
if (!gridDragGrids.Contains(this))
|
|
{
|
|
gridDragGrids.Add(this);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
List<GridDragGrid> gridDragGrids = new List<GridDragGrid>();
|
|
gridDragGrids.Add(this);
|
|
StaticControl.GridViewDragGridDic.Add(sourceView, gridDragGrids);
|
|
}
|
|
if (StaticControl.TargetViewDragGridDic.ContainsKey(targetView))
|
|
{
|
|
List<GridDragGrid> gridDragGrids = StaticControl.TargetViewDragGridDic[targetView];
|
|
if (!gridDragGrids.Contains(this))
|
|
{
|
|
gridDragGrids.Add(this);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
List<GridDragGrid> gridDragGrids = new List<GridDragGrid>();
|
|
gridDragGrids.Add(this);
|
|
StaticControl.TargetViewDragGridDic.Add(targetView, gridDragGrids);
|
|
}
|
|
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// 点击时候判断位置
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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<int>().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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:获取表格选中行数据</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="rowHandles">The row handles.</param>
|
|
/// <returns>DataRow[].</returns>
|
|
private DataRow[] GetGridSelectRows(int[] rowHandles)
|
|
{
|
|
List<DataRow> rows = new List<DataRow>();
|
|
|
|
foreach (int handle in rowHandles)
|
|
{
|
|
DataRow row = this._sourceGridView.GetDataRow(handle);
|
|
rows.Add(row);
|
|
}
|
|
|
|
return rows.ToArray();
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:源表格鼠标按下</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
|
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;
|
|
|
|
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;
|
|
StaticControl.SourceDragGridView = gridView;
|
|
this._hitInfo = hitInfo;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:源表格鼠标移动</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
|
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 >= 0 ? this._hitInfo.RowHandle : 0);
|
|
if (row != null)
|
|
{
|
|
gridView.GridControl.DoDragDrop(row, DragDropEffects.Move);
|
|
}
|
|
this._hitInfo = null;
|
|
DXMouseEventArgs.GetMouseArgs(e).Handled = true;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:源表格鼠标放开</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
|
protected void sourceGridView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
|
|
{
|
|
StaticControl.SourceDragGridView = null;
|
|
this._draging = false;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:移动到目标表格上</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
|
|
private void gridControl_DragOver(object sender, DragEventArgs e)
|
|
{
|
|
GridControl gridControl = (GridControl)sender;
|
|
GridView gridView = gridControl.MainView as GridView;
|
|
this._draging = true;
|
|
if (StaticControl.SourceDragGridView != null && StaticControl.GridViewDragGridDic.ContainsKey(StaticControl.SourceDragGridView))
|
|
{
|
|
List<GridDragGrid> gridDragGrids = StaticControl.GridViewDragGridDic[StaticControl.SourceDragGridView];
|
|
foreach (GridDragGrid item in gridDragGrids)
|
|
{
|
|
if (item != null && item._targetGridView == gridView)
|
|
{
|
|
item._draging = true;
|
|
}
|
|
else
|
|
{
|
|
item._draging = false;
|
|
}
|
|
}
|
|
}
|
|
if (StaticControl.TargetViewDragGridDic.ContainsKey(gridView))
|
|
{
|
|
List<GridDragGrid> gridDragGrids = StaticControl.TargetViewDragGridDic[gridView];
|
|
foreach (GridDragGrid item in gridDragGrids)
|
|
{
|
|
if (item != null && StaticControl.SourceDragGridView != null && item._sourceGridView == StaticControl.SourceDragGridView)
|
|
{
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:离开目标表格</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
|
|
private void gridControl_DragLeave(object sender, EventArgs e)
|
|
{
|
|
this._draging = false;
|
|
|
|
//离开目标表格后,左侧表格开启多选功能
|
|
if(TargetTableMultiSelect) this._targetGridView.OptionsSelection.MultiSelect = true;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:移动到目标表格</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
|
|
protected void gridControl_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
|
|
{
|
|
//拖拽时,左侧表格关闭多选功能
|
|
if (TargetTableMultiSelect) this._targetGridView.OptionsSelection.MultiSelect = false;
|
|
|
|
if (isAllowDrag)
|
|
{
|
|
e.Effect = DragDropEffects.Move;
|
|
}
|
|
else
|
|
{
|
|
e.Effect = DragDropEffects.None;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:拖动完成时</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-11-13 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="System.Windows.Forms.DragEventArgs"/> instance containing the event data.</param>
|
|
protected void gridControl_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
|
|
{
|
|
if (this.OnDragComplete != null && this._draging && this.JudgingConditions())
|
|
{
|
|
this._draging = false;
|
|
this.isAllowDrag = true;
|
|
//拖拽完成后,左侧表格开启多选功能
|
|
if (TargetTableMultiSelect) this._targetGridView.OptionsSelection.MultiSelect = true;
|
|
|
|
GridDragGridArgs args = new GridDragGridArgs();
|
|
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 表格第一列添加复选框
|
|
/// <summary>
|
|
/// <para>说明:表格第一列添加复选框</para>
|
|
/// <para>创建人:唐德馨</para>
|
|
/// <para>创建日期:2021-07-08 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="gridColumn">The grid columnFilterPopupChecke.</param>
|
|
public static void GridAddCheckBox(GridView gridview)
|
|
{
|
|
gridview.OptionsSelection.MultiSelect = true;
|
|
gridview.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CheckBoxRowSelect;
|
|
gridview.OptionsSelection.EnableAppearanceFocusedCell = false;
|
|
gridview.FocusedRowChanged += OnGridViewFocusedRowChanged;
|
|
//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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 焦点行改变时
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private static void OnGridViewFocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
GridView gridView = (GridView)sender;
|
|
Point point = gridView.GridControl.PointToClient(System.Windows.Forms.Control.MousePosition);
|
|
GridHitInfo hitInfo = gridView.CalcHitInfo(point);
|
|
if (hitInfo.Column!=null&& hitInfo.Column.Caption.Equals("Selection") && hitInfo.Column.FieldName.Equals("DX$CheckboxSelectorColumn") && hitInfo.Column.VisibleIndex == 0)
|
|
{
|
|
object cellValue = gridView.GetFocusedRowCellValue(hitInfo.Column);
|
|
if (cellValue is false)
|
|
{
|
|
gridView.SetRowCellValue(hitInfo.RowHandle, hitInfo.Column, true);
|
|
}
|
|
else if (cellValue is true)
|
|
{
|
|
gridView.SetRowCellValue(hitInfo.RowHandle, hitInfo.Column, false);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region 树表格第一列添加复选框
|
|
public static void TreeListAddCheckBox(TreeList treeList)
|
|
{
|
|
treeList.OptionsView.ShowCheckBoxes = true;
|
|
treeList.OptionsSelection.MultiSelect = true;
|
|
treeList.OptionsSelection.MultiSelectMode = TreeListMultiSelectMode.RowSelect;
|
|
treeList.OptionsSelection.InvertSelection = true;
|
|
treeList.OptionsSelection.UseIndicatorForSelection = true;
|
|
treeList.OptionsSelection.EnableAppearanceFocusedCell = false;
|
|
// 绑定节点勾选状态变更事件
|
|
treeList.AfterCheckNode += OnTreeListAfterCheckNode;
|
|
|
|
// 遍历所有列,设置需要自定义汇总的列
|
|
foreach (TreeListColumn column in treeList.Columns)
|
|
{
|
|
GridColumnModel columnModel = (GridColumnModel)column.Tag;
|
|
if (treeList.Columns.IndexOf(column) > 0 && columnModel != null && columnModel.CanSum)
|
|
{
|
|
column.SummaryFooter = DevExpress.XtraTreeList.SummaryItemType.Custom;
|
|
column.RowFooterSummary = DevExpress.XtraTreeList.SummaryItemType.Custom;
|
|
//SummaryItem groupSummaryItem = treeList.GroupSummary.FirstOrDefault(n => n.FieldName == column.FieldName);
|
|
//if (groupSummaryItem != null)
|
|
//{
|
|
// groupSummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Custom;
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 节点勾选状态变更事件处理方法
|
|
private static void OnTreeListAfterCheckNode(object sender, NodeEventArgs e)
|
|
{
|
|
TreeList treeList = sender as TreeList;
|
|
if (treeList == null) return;
|
|
|
|
// 更新子节点状态
|
|
UpdateChildNodes(e.Node, e.Node.Checked);
|
|
|
|
// 更新父节点状态
|
|
UpdateParentNode(e.Node);
|
|
}
|
|
|
|
// 递归更新子节点的选中状态
|
|
private static void UpdateChildNodes(TreeListNode node, bool check)
|
|
{
|
|
foreach (TreeListNode childNode in node.Nodes)
|
|
{
|
|
childNode.Checked = check;
|
|
UpdateChildNodes(childNode, check);
|
|
}
|
|
}
|
|
|
|
// 更新父节点的选中状态
|
|
private static void UpdateParentNode(TreeListNode node)
|
|
{
|
|
TreeListNode parentNode = node.ParentNode;
|
|
if (parentNode == null) return;
|
|
|
|
bool allChecked = true;
|
|
bool allUnchecked = true;
|
|
|
|
// 检查所有兄弟节点的状态
|
|
foreach (TreeListNode siblingNode in parentNode.Nodes)
|
|
{
|
|
if (siblingNode.Checked)
|
|
allUnchecked = false;
|
|
else
|
|
allChecked = false;
|
|
|
|
if (!allChecked && !allUnchecked)
|
|
break;
|
|
}
|
|
|
|
// 根据兄弟节点状态设置父节点状态
|
|
parentNode.Checked = allChecked;
|
|
parentNode.CheckState = allChecked ? CheckState.Checked :
|
|
(allUnchecked ? CheckState.Unchecked : CheckState.Indeterminate);
|
|
|
|
// 递 |