/******************************
* 说明:自定义表格控件
* 创建人:龚宇超
* 创建日期:2017-08-21
* 修改人:
* 修改日期:
* 修改备注:
* 版本:1.0.0.0
******************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using Lskj.Control.Model;
using DevExpress.Data;
using DevExpress.XtraEditors.Controls;
using Lskj.Business;
using Lskj.Business.Impl;
using Lskj.Util;
using Lskj.Control.Properties;
using Lskj.Model;
using Lskj.Data;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.IO;
using DevExpress.XtraPrinting;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Menu;
using DevExpress.Utils.Menu;
using Lskj.Control.MultiGridLookUp;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System.Reflection;
using DevExpress.Data.Filtering;
using Lskj.Core;
using System.Text.RegularExpressions;
using DevExpress.XtraGrid.Views.BandedGrid;
using Lskj.Control.MultiModelLookUp;
using Lskj.Control.HighBeaterInvoice;
using DevExpress.XtraGrid.Drawing;
using DevExpress.XtraPrinting.BarCode.Native;
using Spire.Barcode;
using System.Threading.Tasks;
using System.Collections;
using Newtonsoft.Json;
using System.Net;
using System.Data.Common;
using Lskj.Control.SpecialDate;
namespace Lskj.Control
{
///
/// 自定义表格控件
///
public partial class GridControlEx : UserControl
{
///
/// 说明:缓存计划行
/// 时间:2024-5-22
///
public DataRow jhrowitem = null;
///
/// 说明:第一列获取合计
/// 时间:2022-5-7
///
private bool isFirstVisableCol = true;
///
/// 表格自定义字体
///
private System.Drawing.Text.PrivateFontCollection customFont = new System.Drawing.Text.PrivateFontCollection();
///
/// The _grid enum object
///
private HeaderSiftEnum _columnEnumObj;
protected bool GridSumming = false;
///
/// 表格列头筛选
///
protected GridColumn HeaderColumnSift;
///
/// 列筛选值
///
protected int ColumnSiftValue;
///
/// 筛选类型
///
public enum HeaderSiftEnum
{
///
/// 求和类型
///
Sum,
///
/// 平均类型
///
Avg,
///
/// 最大
///
Max,
/// 最小
///
Min,
///
/// 计数
///
Count
}
///
/// 表格类型
///
/// The grid enum object.
public virtual HeaderSiftEnum SiftEnumObj
{
get
{
return this._columnEnumObj == null ? HeaderSiftEnum.Count : this._columnEnumObj;
}
set
{
this._columnEnumObj = value;
}
}
///
/// 表格类型
///
/// The grid enum object.
public virtual DevExpress.XtraEditors.PanelControl plbuttomObj
{
get
{
return this.pl_buttom;
}
}
public List ObjmControlList
{
get
{
return ColumnList;
}
}
///
/// 表格列头颜色
///
protected string HeaderColor = string.IsNullOrWhiteSpace(SystemInfo.Instance.HeaderColor) ? "#A0A0A0" : SystemInfo.Instance.HeaderColor;
///
/// 必填颜色
///
protected Color RequiredForceColor = string.IsNullOrEmpty(SystemInfo.Instance.ControlRequiredColor) ? Color.Blue : ColorTranslator.FromHtml(SystemInfo.Instance.ControlRequiredColor);
///
/// The reset colum primary key
///
protected string CustomColumKey;
///
/// 缓存列对象
///
protected List ColumnList = new List();
///
/// 缓存自动填充值列对象
///
protected List AutoPadDataReleColList = new List();
///
/// 表格行颜色数据源
///
protected DataTable GridRowColorsTable;
///
/// 表格行颜色数据源
///
protected DataTable ChartTable = new DataTable();
protected string CurrentOperColumnKey = string.Empty;
protected GridColumnModel CurrentOperModel = null;
protected GridView CurrentOperGridView = null;
public List ImageList = new List();
///
/// 外部设置的右键菜单数据,用于创建每行操作列。
///
private DataTable rightMenuButtonTable;
private bool gridColumnsInitialized;
///
/// 新版模块选择框缓存。同一关联模块的单选和多选界面分别复用。
///
private readonly Dictionary selectReturnModelLookUpCache =
new Dictionary(StringComparer.OrdinalIgnoreCase);
///
/// 新版模块选择框显示数据缓存。同一轮列初始化中,相同关联模块只加载一次显示数据。
///
private readonly Dictionary selectReturnDisplaySourceCache =
new Dictionary(StringComparer.OrdinalIgnoreCase);
protected bool mLoading = false;
protected Timer mTimer = new Timer();
private bool _gridResourcesDisposed;
private const int BarcodeImageCacheLimit = 200;
private readonly Dictionary _barcodeImageCache =
new Dictionary(StringComparer.Ordinal);
private readonly Queue _barcodeImageOrder = new Queue();
private readonly Dictionary _rightMenuButtonImages =
new Dictionary(StringComparer.OrdinalIgnoreCase);
///
/// 配置有数据源的控件对象
///
protected List mControlList = new List();
///
/// 数据源任务对象
///
public Dictionary> mControlSourceDic = new Dictionary>();
///
/// 计算值
///
public decimal customSum;
///
/// 计算配置求和值
///
public string SumText;
///
/// 求和列的虚拟表
///
public DataRow sumdr;
///
/// 是否为PubSpec模块
///
public bool IsSpecModel = false;
///
/// 禁用控件是否改变颜色
///
public bool EnabledChangeColor = true;
///
/// 上一次查询SQL
///
public string LastSearchSql = string.Empty;
///
/// 上一次点击搜索按钮查询SQL
///
public string LastSearchButtonhSql = string.Empty;
///
/// 调用动态链接库默认传递参数
///
public DynamicModel Model;
///
/// 系统模块实体对象
///
/// The system model.
public ModuleModel SysModel { get; set; }
///
/// 合计弹出浮窗
///
public SumPanelControl sumPanel;
///
/// 单据主表控件、基础档案查询条件控件等等
///
public MyControl ParentControl;
///
/// Gets or sets the adapter object.
///
/// The adapter object.
public DbDataAdapter AdapterObj { get; set; }
///
/// Gets the grid view.
///
/// The grid view.
public virtual GridView GridView { get { return gridView; } }
///
/// Gets the grid control.
///
/// The grid control.
public virtual GridControl GridControl { get { return gridControl; } }
///
/// 下拉框数据源绑定完成后执行
///
public event EventHandler OnDataSourceBindCallBack;
///
/// Delegate ParseGridDataEventHandler
///
/// The row item.
public delegate void ParseGridDataEventHandler(DataRow rowItem);
///
/// 粘贴表格数据时调用
///
public event ParseGridDataEventHandler OnParseGridDataCallBack;
///
/// 所有数据粘贴完成后调用
///
public event EventHandler OnAllAfterParseCallBack;
///
/// Delegate GridColumnCallDLL
///
/// The row item.
public delegate void GridColumnCallDLL(string type);
///
/// 表格列右键调用dll用
///
public event GridColumnCallDLL OnGridColumnCallDLL;
///
/// 表格列为会计模块
///
private AmountCellRender cellHelper;
private ToolTipController _mainGridToolTipController;
///
/// 合并求和时,前一行的内容
///
public decimal BeforeClass;
///
/// 合并求和时,前一行合并组名
///
public string BeforeGroupName;
///
/// 界面关闭时
///
public event EventHandler OnPopupClosed;
///
/// 默认自定义控件
///
public PopupContainerEdit PopupEdit;
///
/// 缓存筛选数据
///
private Dictionary> _CheckedList = new Dictionary>();
//修改过的单元格
public Dictionary modifyThePosition = new Dictionary();
///
/// 右键菜单类
///
public BaseRightMenu gridViewRightMenu;
///
/// 构建表格列的数据源
///
public DataTable InitGridColumsTab;
///
/// 上次选中行
///
public int LastFocusedRowHandle;
///
/// 是否加载导出
///
public bool ExportPermission = true;
///
/// 当前选中行数组
///
private int[] SelectedRows = new int[0];
///
/// 修改条件
///
public string ModifyCond = string.Empty;
///
///超链接点击回调
///
public event EventHandler OnAfterLinkCilk;
///
/// 表格粘贴行后回调
///
public event EventHandler OnAfterPasting;
//模块返回id框(text)的数据源
public Dictionary ModuleEnclosure = new Dictionary();
///
/// 保存时需要验证是否重复的列
///
public List RepeatedVerificationNames = new List();
///
/// 触发计算关联后,计算合计
///
public bool RefreshTotal = true;
///
/// 排序关联值
///
private string DetailOrderField = string.Empty;
///
/// 冻结列
///
private List FrozenColumns = new List();
private Point m_mouseDownLocation;
private int m_dragHandle;
private int[] dragRowsIndex;
private DragForm m_dragRowShadow;
///
/// 判断明细是否是拖拽中
///
private bool isSortDrag;
///
/// 设置数据源时是否允许清空列
///
public bool AllowClearColumns = true;
///
/// 第一次保存全部的列
///
private List ColumnsList = new List();
///
///复选模式,表格主键
///
public string MultiplePrimary = string.Empty;
///
///选中的行在数据源中的主键值
///
private List SelectedDataSource = new List();
///
/// 禁显列不加载数据源
///
public bool DisableFieldSources;
///
/// 表格重新赋值数据源时,更新的列
///
public string UpdateColumns;
///
/// 聚合多表头控件
///
public BandedGridControlEx CustomGroupBandEx;
///
/// 聚合多表头控件
///
public TreeBandedGridControlEx CustomGroupTreeBandEx;
///
/// 聚合界面是否冻结基本信息列
///
public bool AggregationFreeze;
///
/// 聚合多表头控件选中行
///
public DataRow CustomGroupSelectedRow
{
get
{
DataRow dataRow = null;
if (CustomGroupBandEx != null)
{
dataRow = CustomGroupBandEx.GetViewFocusedDataRow();
}
else if (CustomGroupTreeBandEx != null)
{
dataRow = CustomGroupTreeBandEx.GetViewFocusedDataRow();
}
return dataRow;
}
}
///
/// 导出名不包含日期
///
public bool ExportNameWithoutDate = false;
///
/// 导出名不包含日期
///
public bool IgnoreTab = false;
///
/// 创建动态分组列控件回调
///
public event EventHandler OnBuildGroupedColumnCallBack;
///
/// 记录上一次分组状态,避免重复排序
///
private int _lastGroupCount = 0;
///
/// 分组后根据汇总排序是否完成
///
private bool Completed = true;
///
/// 进度条类型的列名称
///
private List ProgressBarColumn = new List();
///
/// 提示文本 字段(列名,提示信息)
///
public Dictionary PromptMessage = new Dictionary();
///
/// 合计行格式(设置成全局变量,更好的处理语言翻译的情况)
///
public string TotalFormat = Business.Impl.LanguageTranslation.Translatable ? Business.Impl.LanguageTranslation.GetTranslatedText("合计") + ": {0:#,###}" + Business.Impl.LanguageTranslation.GetTranslatedText("行") : "合计: {0:#,###}行";
///
/// 搜索框 手动添加行
///
public Dictionary> ManuallyAddData = new Dictionary>();
///
/// 修改字段
///
public string ModifyFields = string.Empty;
///
/// 必填字段
///
public string RequiredFields = string.Empty;
///
/// 合计显示选中行数
///
public bool TotalQuantity;
///
///新增行
///
/// The sender.
public event ModuleAddLine OnNewLine;
public delegate void ModuleAddLine(string name, List list);
public GridControlEx()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
this.Disposed += OnGridControlExDisposed;
// 自定义选中区域方式
this.gridView.CustomDrawRowIndicator += new RowIndicatorCustomDrawEventHandler(OnGridViewCustomDrawRowIndicator);
this.gridView.SelectionChanged += new SelectionChangedEventHandler(OnGridViewSelectionChanged);
this.gridView.FocusedRowObjectChanged += new FocusedRowObjectChangedEventHandler(OnGridViewFocusedRowObjectChanged);
this.gridView.BeforeLeaveRow += new RowAllowEventHandler(OnGridView_BeforeLeaveRow);
this.gridView.CustomDrawCell += new RowCellCustomDrawEventHandler(OnGridViewCustomDrawCell);
this.gridView.CellValueChanged += new CellValueChangedEventHandler(OnGridViewCellValueChanged);
this.gridView.DragObjectDrop += new DragObjectDropEventHandler(OnGridViewDragObjectDrop);
this.gridView.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(OnGridCustomDrawGroupRow);
this.gridView.KeyDown += new KeyEventHandler(OnGridViewKeyDown);
this.gridView.DoubleClick += new EventHandler(OnGridViewDoubleClick);
this.gridView.Click += new EventHandler(OnGridViewClick);
this.gridView.CellMerge += new CellMergeEventHandler(OnGridViewCellMerge);
this.gridView.CustomSummaryCalculate += new CustomSummaryEventHandler(OnGridViewSummaryCalculate);
this.gridView.OptionsNavigation.EnterMoveNextColumn = true;
this.gridView.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDownFocused;
this.SetGridRowHeightAndFont();
this.gridView.ShowFilterPopupCheckedListBox += new FilterPopupCheckedListBoxEventHandler(OnShowFilterPopupCheckedListBox);//(修复筛选条件)
//this.gridView.ValidateRow += OnGridView_ValidateRow;
this.gridView.DataSourceChanged += new EventHandler(OnGridDataSourceChanged);
cellHelper = new AmountCellRender(this.gridView);
//鼠标悬浮提示框
_mainGridToolTipController = new ToolTipController();
_mainGridToolTipController.AutoPopDelay = 1000000;//提示框显示时间 10分钟
_mainGridToolTipController.BeforeShow += new ToolTipControllerBeforeShowEventHandler(MainGvTool_BeforeShow);
this.gridControl.ToolTipController = _mainGridToolTipController;
//this.gridView.PopupMenuShowing += new PopupMenuShowingEventHandler(OnGridView_PopupMenuShowing);//添加Footer菜单选项
this.gridView.Click += new EventHandler(OnGridView_Click);//点击复制Footer数据信息
if (SystemInfo.Instance.SelectColorHighlight)
{
this.gridView.RowCellStyle += GridView_RowCellStyle;
}
if (SystemInfo.Instance.GroupRowFontSizeDelta > 0)
{
this.gridView.Appearance.GroupRow.FontSizeDelta = SystemInfo.Instance.GroupRowFontSizeDelta;
}
if (SystemInfo.Instance.DisplayCheckboxSelection)
{
this.gridView.CustomRowFilter += GridView_CustomRowFilter;
}
#region LabPicUrl控件实现事件
//this.gridView.CustomUnboundColumnData += OnGridViewCustomUnboundColumnData;
//this.gridView.DoubleClick += OnShowImageDoubleClick;
LabPicUrlPreview.Attach(this.gridView); // 必须
LabPicUrlPreview.AttachPreview(this.gridView); // 新增(双击预览)
#endregion
if (SystemInfo.Instance.GroupSpecialMode) this.gridView.EndGrouping += GridView_EndGrouping;
this.gridView.ShowingEditor += GridView_ShowingEditor;
this.gridView.MouseDown += GridView_MouseDown;
//绑定滚轮横向滚动
this.gridView.MouseWheel += GridView_MouseWheel;
}
private DbCommandBuilder _adapterCommandBuilder;
protected static void DisposeDataAdapter(DbDataAdapter adapter)
{
if (adapter == null)
{
return;
}
DbCommand[] commands =
{
adapter.SelectCommand,
adapter.InsertCommand,
adapter.UpdateCommand,
adapter.DeleteCommand
};
foreach (DbCommand command in commands)
{
try
{
command?.Dispose();
}
catch
{
// Resource cleanup must not break control disposal or a new query.
}
}
try
{
adapter.Dispose();
}
catch
{
// Resource cleanup must not break control disposal or a new query.
}
}
protected void SetAdapterCommandBuilder(DbDataAdapter adapter)
{
DisposeAdapterCommandBuilder();
if (adapter == null)
{
return;
}
_adapterCommandBuilder = SqlHelper.dbFactory.CreateCommandBuilder();
_adapterCommandBuilder.DataAdapter = adapter;
}
protected void DisposeAdapterCommandBuilder()
{
DbCommandBuilder commandBuilder = _adapterCommandBuilder;
_adapterCommandBuilder = null;
try
{
commandBuilder?.Dispose();
}
catch
{
// Resource cleanup must not break control disposal or a new query.
}
}
internal void ReleaseResourcesForDispose()
{
DisposeGridResources();
}
private void DetachGridViewEventHandlers()
{
if (gridView == null)
{
return;
}
gridView.CustomDrawRowIndicator -= OnGridViewCustomDrawRowIndicator;
gridView.SelectionChanged -= OnGridViewSelectionChanged;
gridView.FocusedRowObjectChanged -= OnGridViewFocusedRowObjectChanged;
gridView.BeforeLeaveRow -= OnGridView_BeforeLeaveRow;
gridView.CustomDrawCell -= OnGridViewCustomDrawCell;
gridView.CellValueChanged -= OnGridViewCellValueChanged;
gridView.DragObjectDrop -= OnGridViewDragObjectDrop;
gridView.CustomDrawGroupRow -= OnGridCustomDrawGroupRow;
gridView.KeyDown -= OnGridViewKeyDown;
gridView.DoubleClick -= OnGridViewDoubleClick;
gridView.Click -= OnGridViewClick;
gridView.Click -= OnGridView_Click;
gridView.CellMerge -= OnGridViewCellMerge;
gridView.CustomSummaryCalculate -= OnGridViewSummaryCalculate;
gridView.ShowFilterPopupCheckedListBox -= OnShowFilterPopupCheckedListBox;
gridView.DataSourceChanged -= OnGridDataSourceChanged;
gridView.RowCellStyle -= GridView_RowCellStyle;
gridView.CustomRowFilter -= GridView_CustomRowFilter;
gridView.EndGrouping -= GridView_EndGrouping;
gridView.ShowingEditor -= GridView_ShowingEditor;
gridView.MouseDown -= GridView_MouseDown;
gridView.MouseWheel -= GridView_MouseWheel;
gridView.PopupMenuShowing -= OnGridViewPopupMenuShowing;
gridView.EndSorting -= OnGridViewEndSorting;
LabPicUrlPreview.Detach(gridView);
}
private void DisposeGridResources()
{
if (_gridResourcesDisposed)
{
return;
}
_gridResourcesDisposed = true;
if (cellHelper != null)
{
cellHelper.Dispose();
cellHelper = null;
}
StopColumnSourceTimer();
if (mTimer != null)
{
mTimer.Dispose();
mTimer = null;
}
try
{
if (GridControl != null)
{
GridControl.DataSource = null;
}
}
catch
{
// The inner DevExpress control may already be disposed.
}
DisposeColumnImageCaches(ColumnList);
DisposeRightMenuButtonImages();
ClearBarcodeImageCache();
DisposeAdapterCommandBuilder();
DisposeDataAdapter(AdapterObj);
AdapterObj = null;
mControlSourceDic.Clear();
mControlList.Clear();
ColumnList.Clear();
AutoPadDataReleColList.Clear();
FrozenColumns.Clear();
ImageList.Clear();
RepeatedVerificationNames.Clear();
PromptMessage.Clear();
DetachGridViewEventHandlers();
DisposeGridViews();
DisposeRepositoryItems();
gridViewRightMenu?.ReleaseResourcesForDispose();
gridViewRightMenu = null;
ParentControl = null;
Model = null;
SysModel = null;
if (_mainGridToolTipController != null)
{
_mainGridToolTipController.BeforeShow -= MainGvTool_BeforeShow;
if (gridControl != null)
{
gridControl.ToolTipController = null;
}
_mainGridToolTipController.Dispose();
_mainGridToolTipController = null;
}
if (customFont != null)
{
customFont.Dispose();
customFont = null;
}
}
private void PrepareForColumnRebuild()
{
StopColumnSourceTimer();
DisposeColumnImageCaches(ColumnList);
DisposeRightMenuButtonImages();
ClearBarcodeImageCache();
GridView.Columns.Clear();
GridView.GroupSummary.Clear();
GridView.OptionsView.ShowFooter = false;
DisposeRepositoryItems();
ColumnList.Clear();
AutoPadDataReleColList.Clear();
FrozenColumns.Clear();
ImageList.Clear();
RepeatedVerificationNames.Clear();
PromptMessage.Clear();
mControlList.Clear();
mControlSourceDic.Clear();
isFirstVisableCol = true;
mLoading = false;
}
private void DisposeRepositoryItems()
{
RepositoryItem[] repositoryItems;
try
{
if (gridControl == null)
{
return;
}
repositoryItems = gridControl.RepositoryItems
.Cast()
.ToArray();
}
catch
{
// The inner control may already have released its repository collection.
return;
}
DisposeRepositoryButtonForms(repositoryItems);
try
{
gridControl.RepositoryItems.Clear();
}
catch
{
// Continue disposing the captured items.
}
foreach (RepositoryItem repositoryItem in repositoryItems)
{
try
{
repositoryItem.Dispose();
}
catch
{
// Continue releasing the remaining repository items.
}
}
}
private static void DisposeRepositoryButtonForms(IEnumerable repositoryItems)
{
HashSet