/******************************
* 说明:自定义表格控件
* 创建人:龚宇超
* 创建日期: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();
protected bool mLoading = false;
protected Timer mTimer = new Timer();
///
/// 配置有数据源的控件对象
///
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;
///
/// 合并求和时,前一行的内容
///
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.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);
//鼠标悬浮提示框
ToolTipController MainGvTool = new ToolTipController();
MainGvTool.AutoPopDelay = 1000000;//提示框显示时间 10分钟
MainGvTool.BeforeShow += new ToolTipControllerBeforeShowEventHandler(MainGvTool_BeforeShow);
this.gridControl.ToolTipController = MainGvTool;
//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;
}
///
/// 保存前反写多表头数据源
///
public void callbackUpdData()
{
try
{
if (CustomGroupBandEx != null)
{
// 获取动态多表头的Grid控件数据源 (假设为DataTable)
DataTable updatedTable = CustomGroupBandEx.gridControl.DataSource as DataTable;
if (updatedTable == null) return;
// 获取原始的GridControl的数据源
DataTable originalDataTable = this.gridControl.DataSource as DataTable;
if (originalDataTable == null) return;
// 获取分组相关的列定义
GridColumnModel GroupPrimary = ColumnList.FirstOrDefault(o => o.IsCustomGroupPrimary);
GridColumnModel GroupFieldprimary = ColumnList.FirstOrDefault(o => o.IsCustomGroupField == 1);
List GroupField = ColumnList.Where(o => o.IsCustomGroupField == 2).ToList();
if (GroupPrimary == null || GroupFieldprimary == null || !GroupField.Any()) return;
// 遍历每一行更新回原始数据表
foreach (DataRow updatedRow in updatedTable.Rows)
{
string primaryKeyValue = updatedRow[GroupPrimary.FieldName]?.ToString();
// 根据主键定位原始表中的行集合
var originalRows = originalDataTable.AsEnumerable()
.Where(row => row.Field(GroupPrimary.FieldName) == primaryKeyValue).ToList();
if (!originalRows.Any()) continue; // 若没有匹配的行,跳过
// 遍历动态表头的Band及子列
foreach (GridBand band in CustomGroupBandEx.BandedView.Bands)
{
if (band.Caption == "基本信息") continue; // 跳过普通列Band
string groupFieldPrimaryValue = band.Caption;
foreach (BandedGridColumn column in band.Columns)
{
var subField = column.Tag as GridColumnModel;
if (subField == null) continue;
string dynamicFieldName = column.FieldName;
object updatedValue = updatedRow[dynamicFieldName];
// 精确匹配原数据源对应的单行
DataRow targetRow = originalRows.FirstOrDefault(r =>
r.Field(GroupFieldprimary.FieldName) == groupFieldPrimaryValue);
// 若找到行,则更新该行对应的字段
if (targetRow != null && originalDataTable.Columns.Contains(subField.FieldName))
{
targetRow[subField.FieldName] = updatedValue;
}
else if (targetRow == null)
{
// 若没找到,说明可能为新增数据行,插入新行(若业务需要)
DataRow newOriginalRow = originalDataTable.NewRow();
newOriginalRow[GroupPrimary.FieldName] = primaryKeyValue;
newOriginalRow[GroupFieldprimary.FieldName] = groupFieldPrimaryValue;
foreach (var col in ColumnList.Where(x => !x.IsCustomGroupField.Equals(1) && !x.IsCustomGroupPrimary))
{
if (updatedTable.Columns.Contains(col.FieldName) && originalDataTable.Columns.Contains(col.FieldName))
{
newOriginalRow[col.FieldName] = updatedRow[col.FieldName];
}
}
if (originalDataTable.Columns.Contains(subField.FieldName))
{
newOriginalRow[subField.FieldName] = updatedValue;
}
originalDataTable.Rows.Add(newOriginalRow);
}
}
}
}
}
else if (CustomGroupTreeBandEx != null)
{
// 获取动态多表头的Grid控件数据源 (假设为DataTable)
DataTable updatedTable = CustomGroupTreeBandEx.TreeListObj.DataSource as DataTable;
if (updatedTable == null) return;
// 获取原始的GridControl的数据源
DataTable originalDataTable = this.gridControl.DataSource as DataTable;
if (originalDataTable == null) return;
// 获取分组相关的列定义
GridColumnModel GroupPrimary = ColumnList.FirstOrDefault(o => o.IsCustomGroupPrimary);
GridColumnModel GroupFieldprimary = ColumnList.FirstOrDefault(o => o.IsCustomGroupField == 1);
List GroupField = ColumnList.Where(o => o.IsCustomGroupField == 2).ToList();
if (GroupPrimary == null || GroupFieldprimary == null || !GroupField.Any()) return;
// 遍历每一行更新回原始数据表
foreach (DataRow updatedRow in updatedTable.Rows)
{
string primaryKeyValue = updatedRow[GroupPrimary.FieldName]?.ToString();
// 根据主键定位原始表中的行集合
var originalRows = originalDataTable.AsEnumerable()
.Where(row => row.Field(GroupPrimary.FieldName) == primaryKeyValue).ToList();
if (!originalRows.Any()) continue; // 若没有匹配的行,跳过
// 遍历动态表头的Band及子列
foreach (DevExpress.XtraTreeList.Columns.TreeListBand band in CustomGroupTreeBandEx.TreeListObj.Bands)
{
if (band.Caption == "基本信息") continue; // 跳过普通列Band
string groupFieldPrimaryValue = band.Caption;
foreach (DevExpress.XtraTreeList.Columns.TreeListColumn column in band.Columns)
{
var subField = column.Tag as GridColumnModel;
if (subField == null) continue;
string dynamicFieldName = column.FieldName;
object updatedValue = updatedRow[dynamicFieldName];
// 精确匹配原数据源对应的单行
DataRow targetRow = originalRows.FirstOrDefault(r =>
r.Field(GroupFieldprimary.FieldName) == groupFieldPrimaryValue);
// 若找到行,则更新该行对应的字段
if (targetRow != null && originalDataTable.Columns.Contains(subField.FieldName))
{
targetRow[subField.FieldName] = updatedValue;
}
else if (targetRow == null)
{
// 若没找到,说明可能为新增数据行,插入新行(若业务需要)
DataRow newOriginalRow = originalDataTable.NewRow();
newOriginalRow[GroupPrimary.FieldName] = primaryKeyValue;
newOriginalRow[GroupFieldprimary.FieldName] = groupFieldPrimaryValue;
foreach (var col in ColumnList.Where(x => !x.IsCustomGroupField.Equals(1) && !x.IsCustomGroupPrimary))
{
if (updatedTable.Columns.Contains(col.FieldName) && originalDataTable.Columns.Contains(col.FieldName))
{
newOriginalRow[col.FieldName] = updatedRow[col.FieldName];
}
}
if (originalDataTable.Columns.Contains(subField.FieldName))
{
newOriginalRow[subField.FieldName] = updatedValue;
}
originalDataTable.Rows.Add(newOriginalRow);
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("保存失败:" + ex.Message);
}
}
///
/// 说明:单元格合并
/// 创建人:王一帆
/// 创建日期:
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The sender.
/// The instance containing the event data.
protected void OnGridViewCellMerge(object sender, CellMergeEventArgs e)
{
try
{
GridColumn column = e.Column;
GridColumnModel model = column.Tag as GridColumnModel;
if (string.IsNullOrEmpty(model.MergeGroup)) return;
string[] fields = model.MergeGroup.Trim(',').Split(',');
bool handled = false;//默认合并
DataTable table = gridView.GridControl.DataSourceTable();
for (int i = 0; i < fields.Length; i++)
{
int row1 = e.RowHandle1;
int row2 = e.RowHandle2;
string McColumn = fields[i] + "";//关联条件列
if (!table.Columns.Contains(McColumn)) break;
string value1 = gridView.GetDataRow(row1)[McColumn].ToString();
string value2 = gridView.GetDataRow(row2)[McColumn].ToString();
if (value1 != value2)
{
//e.Handled = true;
handled = true;//如果配置多个,有一个分组上下值不同,则不会合并
}
//else
//{
// //e.Handled = false;
//}
}
e.Handled = handled;
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex, Model.ModuleCode);
MessageUtil.Show(Message, ex.Message);
LogHelper.Instance.WriteError(ex);
}
}
private string GetMergeGroupValue(GridColumnModel model, DataRow row)
{
if (model == null || row == null || string.IsNullOrEmpty(model.MergeGroup)) return string.Empty;
string[] fields = model.MergeGroup.Trim(',').Split(',');
StringBuilder groupValue = new StringBuilder();
foreach (string item in fields)
{
string fieldName = (item + "").Trim();
if (string.IsNullOrEmpty(fieldName) || !row.Table.Columns.Contains(fieldName)) continue;
groupValue.Append(fieldName);
groupValue.Append("=");
groupValue.Append(row[fieldName] + "");
groupValue.Append("|");
}
return groupValue.ToString();
}
///
/// 说明:表格列求和触发事件
/// 创建人:王一帆
/// 创建日期:2020-09-22
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The sender.
/// The instance containing the event data.
protected virtual void OnGridViewSummaryCalculate(object sender, CustomSummaryEventArgs e)
{
try
{
bool isSum = false;
//DataRow NRow = e.Row != null ? (e.Row as DataRowView).Row : null;
var gridView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
GridSummaryItem gridSummaryItem = e.Item as GridSummaryItem;
GridColumn gridColumn = gridView.Columns[gridSummaryItem.FieldName] as GridColumn;
GridColumnModel model = gridColumn.Tag as GridColumnModel;
GridViewInfo info = gridView.GetViewInfo() as GridViewInfo;
switch (e.SummaryProcess)
{
//calculation entry point 计算开始时
case DevExpress.Data.CustomSummaryProcess.Start:
customSum = 0;
SumText = model.sumCalc;
sumdr = null;
if (!string.IsNullOrEmpty(model.sumCalc))
{
DataTable SumTab = new DataTable();
List condition = ReplaceHelper.GetParamFields(model.sumCalc);
foreach (string item in condition)
{
string field = item.Replace("{", "").Replace("}", "");
SumTab.Columns.Add(field);
}
sumdr = SumTab.NewRow();
}
break;
//consequent calculations 计算中自定义计算方法
case CustomSummaryProcess.Calculate:
bool isAddCheck = gridView.OptionsSelection.MultiSelect == true && gridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.CheckBoxRowSelect && gridView.OptionsSelection.EnableAppearanceFocusedCell == false ? true : false;
DataRow gridRow = null;
if (isAddCheck && this.SelectedRows != null && this.SelectedRows.Count() > 0)
{
if (this.SelectedRows != null && this.SelectedRows.Contains(e.RowHandle))
{
gridRow = gridView.GetDataRow(e.RowHandle);
}
else
{
break;
}
}
else
{
gridRow = gridView.GetDataRow(e.RowHandle);
}
isSum = !string.IsNullOrWhiteSpace(model.SumCond) ? "true".Equals(EvalHelper.Eval(ReplaceHelper.ReplaceEvalCond(ReplaceHelper.ReplaceRowParam(gridRow, model.SumCond))) + "", StringComparison.OrdinalIgnoreCase) : true;
if (!string.IsNullOrEmpty(model.sumCalc))
{
foreach (DataColumn col in sumdr.Table.Columns)
{
sumdr[col.ColumnName] = Convert.ToDecimal(string.IsNullOrEmpty(sumdr[col.ColumnName] + "") ? "0" :
sumdr[col.ColumnName] + "") + Convert.ToDecimal(string.IsNullOrEmpty(gridRow[col.ColumnName] + "") ? "0" : gridRow[col.ColumnName] + "");
}
}
else
{
if (model.isSumMerge && !string.IsNullOrEmpty(e.FieldValue + ""))
{
if (!string.IsNullOrEmpty(model.MergeGroup))
{
string currentGroupName = GetMergeGroupValue(model, gridRow);
bool isMergrGroup = !string.IsNullOrEmpty(currentGroupName) && e.RowHandle != 0 ? currentGroupName.Equals(BeforeGroupName) : false;
if (model.CanMerge && BeforeClass == Convert.ToDecimal(e.FieldValue) && isMergrGroup)
{
BeforeClass = Convert.ToDecimal(e.FieldValue);
BeforeGroupName = currentGroupName;
}
else
{
BeforeClass = Convert.ToDecimal(e.FieldValue);
BeforeGroupName = currentGroupName;
if (isSum || String.IsNullOrEmpty(model.SumCond)) customSum += Convert.ToDecimal(e.FieldValue);
}
}
else
{
if (model.CanMerge && BeforeClass == Convert.ToDecimal(e.FieldValue))
{
BeforeClass = Convert.ToDecimal(e.FieldValue);
}
else
{
BeforeClass = Convert.ToDecimal(e.FieldValue); BeforeGroupName = !string.IsNullOrEmpty(model.MergeGroup) && gridRow.Table.Columns.Contains(model.MergeGroup) ? gridRow[model.MergeGroup] + "" : "";
if (isSum || String.IsNullOrEmpty(model.SumCond)) customSum += Convert.ToDecimal(e.FieldValue);
}
}
}
if (model.CanSum && isSum)
{
if (BeforeClass != Convert.ToDecimal(e.FieldValue))
{
customSum += Convert.ToDecimal(e.FieldValue);
}
}
}
break;
//final summary value 计算结束
case CustomSummaryProcess.Finalize:
if (gridView.GridControl.DataSourceTable().Rows.Count == 0) break;
if (sumdr != null)
{
foreach (DataColumn col in sumdr.Table.Columns)
{
string newresult = sumdr[col.ColumnName] + "";
GridColumn newColumn = gridView.Columns[col.ColumnName] as GridColumn;
GridColumnModel newModel = newColumn.Tag as GridColumnModel;
if (!string.IsNullOrEmpty(newModel.DataFormat))
{
//计算后先执行保存小数位数
if (!model.sumCalc.StartsWith("@"))
{
newresult = string.Format("{0:" + newModel.DataFormat + "}", Convert.ToDecimal(string.IsNullOrEmpty(newresult) ? "0" : newresult));
}
}
sumdr[col.ColumnName] = newresult + "";
}
if (this.ParentControl != null)
SumText = ParentControl.ReplaceParentControlValue(model.sumCalc);
SumText = ReplaceHelper.ReplaceRowParam(sumdr, SumText);
string result = string.Empty;
if (SumText.StartsWith("@"))
{
try
{
result = MainImpl.GetResult(SumText.Replace("@", "")) + "";
}
catch (Exception)
{
}
}
else
{
result = EvalHelper.Eval2(ReplaceHelper.ReplaceEvalCond(SumText)) + "";
}
if (result.Equals("NaN") || string.IsNullOrWhiteSpace(result)) result = "0";
if (string.IsNullOrEmpty(model.DataFormat))
{
result = Math.Round(Convert.ToDouble(result), model.Decimals, MidpointRounding.AwayFromZero) + "";
}
else
{
//计算后先执行保存小数位数
result = string.Format("{0:" + model.DataFormat + "}", Convert.ToDecimal(result));
}
//如果是数值类型,就去掉百分号
//if (model.FieldType == 7 && result.Contains('%'))
//{
// result = (double.Parse(result.Replace("%", "")) * 0.01).ToString();
//}
e.TotalValue = result;
}
else
{
e.TotalValue = customSum;
BeforeClass = 0;
BeforeGroupName = "";
}
//汇总数据为当前列展示值相加
if (model.CanSum && model.Summation)
{
if (gridColumn != null)
{
double value = 0;
for (int i = 0; i < gridView.RowCount; i++)
{
string DisplayText = gridView.GetRowCellDisplayText(i, gridColumn.FieldName) + "";
if (!string.IsNullOrWhiteSpace(DisplayText)) value += double.Parse(DisplayText);
}
e.TotalValue = value;
}
}
break;
}
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex, Model.ModuleCode);
MessageUtil.Show(Message, ex.Message);
}
}
///
/// 说明:创建列
/// 创建人:龚宇超
/// 创建日期:2017-08-21
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The dr row.
protected void InitEditColumns(GridColumn column, GridColumnModel model)
{
if (this.DisableFieldSources && model.isVislble)
{
return;
}
if (!string.IsNullOrEmpty(model.SqlSource) && !ControlType.IsNotLoadData(model.FieldType))
{
Task sourceTask = new Task(() =>
{
DataTable dataTable = new DataTable();
using (DbConnection sqlConnection = SqlHelper.dbFactory.CreateConnection())
{
sqlConnection.ConnectionString = SqlHelper._connection.ConnectionString;
using (DbCommand cmd = SqlHelper.dbFactory.CreateCommand())
{
try
{
string sqlValue = BaseImpl.GetDefaultValue(model.SqlSource);
sqlValue = ReplaceHelper.ReplaceParam(sqlValue);
if (sqlConnection.State != ConnectionState.Open)
{
sqlConnection.Open();
}
cmd.Connection = sqlConnection;
cmd.CommandTimeout = 0;
cmd.CommandText = sqlValue;
cmd.CommandType = CommandType.Text;
DbDataAdapter adapter = SqlHelper.dbFactory.CreateDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dataTable);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConnection.Close();
cmd.Dispose();
}
}
}
return dataTable;
});
sourceTask.Start(DataTableExtend.SchedulerUtil);
mControlSourceDic.Add(model, sourceTask);
}
switch (model.FieldType)
{
case ControlType.LabAutoLinefeedText:
InitAutoLinefeedEdit(column, model);
break;
case ControlType.LabPassWordValue:
InitPasswordEdit(column, model);
break;
case ControlType.LabComboxValue:
case ControlType.LabComboxText:
case ControlType.LabComboxValueParam:
case ControlType.LabComboxTextParam:
InitComboxEdit(column, model);
break;
case ControlType.LabProgressBar:
InitProgressEdit(column);
break;
case ControlType.LabCheckBox:
InitCheckEdit(column);
break;
case ControlType.LabAutoCompleteValue:
case ControlType.LabAutoCompleteText:
case ControlType.LabAutoCompleteValueParam:
case ControlType.LabAutoCompleteTextParam:
case ControlType.LabAutoGridValue:
case ControlType.LabAutoGridText:
case ControlType.LabAutoGridValueParam:
case ControlType.LabAutoGridTextParam:
InitAutoSearchEdit(column, model);
break;
case ControlType.LabDate:
case ControlType.LabDateTime:
case ControlType.LabDateTimeShort:
case ControlType.LabShortDate:
InitDateTimeEdit(column, model.FieldType);
break;
case ControlType.LabDateHalfDay:
InitSpecialDate(column, model);
break;
case ControlType.LabTime:
case ControlType.LabShortTime:
InitTimeEdit(column, model.FieldType);
break;
case ControlType.LabCalcText:
InitCalcEdit(column);
break;
case ControlType.LabMemoEdit:
InitMemoEdit(column);
break;
case ControlType.LabRemark:
InitRemarkEdit(column);
break;
case ControlType.LabAccount:
this.cellHelper.AddAmountColumn(column, 11);
break;
case ControlType.LabMultiSelectValue:
case ControlType.LabMultiSelectValueNew:
case ControlType.LabMultiSelectValueParam:
case ControlType.LabMultiSelectText:
case ControlType.LabMultiSelectTextNew:
case ControlType.LabMultiSelectTextParam:
InitMutilEdit(column, model);
break;
case ControlType.LabMulitTreeText:
case ControlType.LabMulitTreeTextParam:
case ControlType.LabMulitTreeValue:
case ControlType.LabMulitTreeValueParam:
InitAutoTreeSearchEdit(column, model, true, false);
break;
case ControlType.LabMultiComTreeboxValue:
case ControlType.LabMultiComTreeboxText:
case ControlType.LabMultiComTreeboxValueParam:
case ControlType.LabMultiComTreeboxTextParam:
InitAutoTreeSearchEdit(column, model, true, true);
break;
case ControlType.LabTreeLastlevelValue:
case ControlType.LabTreeLastlevelText:
case ControlType.LabCheckTreeLastlevelValue:
case ControlType.LabCheckTreeLastlevelText:
InitAutoTreeSearchEdit(column, model, false, false);
break;
case ControlType.LabTreeLookValue:
case ControlType.LabTreeLookText:
case ControlType.LabCheckTreeLookValue:
case ControlType.LabCheckTreeLookText:
InitAutoTreeSearchEdit(column, model, false, true);
break;
case ControlType.LabComboxCheckListValue:
case ControlType.LabComboxCheckListText:
InitMutilComboxEdit(column, model);
break;
//case ControlType.LabQQ:
// column.Tag = "QQ";
// break;
//case ControlType.LabWWW:
// column.Tag = "Brower";
// break;
case ControlType.LabRichEdit:
InitRichEdit(column);
break;
case ControlType.LabPic:
InitPicEdit(column);
break;
case ControlType.LabPicUrl:
//InitPicEditUrl(column, model);
// 用工具类创建图片列(绑定 Tag、Repository、UnboundType)
LabPicUrlPreview.InitPicColumn(this.gridControl, column, model);
break;
case ControlType.LabSelectReturnId:
case ControlType.LabSelectReturnText:
InitSelectReturnId(column, model);
break;
case ControlType.LabSelectReturnIdNew:
case ControlType.LabSelectReturnTextNew:
InitSelectReturnIdNew(column, model);
break;
case ControlType.LabSelectReturnRows:
InitSelectReturnRows(column, model);
break;
case ControlType.LabSelectReturnRowsNew:
InitSelectReturnRowsNew(column, model);
break;
case ControlType.DynamicallyGeneratedSql:
InitSqlGenerate(column, model);
break;
case ControlType.HighShotMeter:
InitHighShotMeterEdit(column, model);
break;
case ControlType.BarCode128B:
InitBarCode128BEdit(column, model);
break;
case ControlType.LabHyperlink:
InitHyperlink(column, model);
break;
case ControlType.LabRightlink:
InitRightlink(column, model);
break;
case ControlType.LabComboxPopupValue:
case ControlType.LabComboxPopupText:
InitComboxPopup(column, model);
break;
case ControlType.LabGridPopupValue:
case ControlType.LabGridPopupText:
//case ControlType.LabGridPopupMutilValue:
//case ControlType.LabGridPopupMutilText:
InitGridPopup(column, model);
break;
case ControlType.LabModuleAddRowsID:
case ControlType.LabModuleAddRowsText:
InitModuleReturnLine(column, model);
break;
case ControlType.LabModuleSelectReturnIdExtended:
case ControlType.LabSearchReturnIdExtended:
InitExtendedReturn(column, model);
break;
default:
break;
}
// 特殊处理
if (ControlType.LabTreeType == model.FieldType && !IsSpecModel)
{
InitComboxEdit(column, model);
}
}
///
/// 说明:设置列格式化
/// 创建人:龚宇超
/// 创建日期:2018-03-21
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The grid column.
/// The model.
protected virtual void SetColumnFormat(GridColumn gridColumn, GridColumnModel model)
{
if (gridColumn == null || model == null) return;
// 格式化数字
if (!string.IsNullOrEmpty(model.DataFormat))
{
if (model.FieldType == ControlType.LabTextInt || model.FieldType == ControlType.LabCalcText || model.FieldType == ControlType.LabAccount)
{
gridColumn.DisplayFormat.FormatType = FormatType.Numeric;
gridColumn.DisplayFormat.FormatString = model.DataFormat;
}
else
{
gridColumn.DisplayFormat.FormatType = FormatType.Custom;
gridColumn.DisplayFormat.FormatString = model.DataFormat;
}
}
// 特殊字段格式化
if (model.CanAccuracy)
{
gridColumn.DisplayFormat.FormatType = FormatType.Numeric;
gridColumn.DisplayFormat.FormatString = "F2";
}
}
///
/// 说明:设置列汇总
/// 创建人:龚宇超
/// 创建日期:2018-03-21
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The grid column.
/// The model.
protected virtual void SetColumnSumming(GridColumn gridColumn, GridColumnModel model)
{
// 设置底部合计
if (gridColumn.VisibleIndex >= 0 && this.isFirstVisableCol)
{
gridView.OptionsView.ShowFooter = true;
gridColumn.Summary.AddRange(new GridSummaryItem[] { new GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count, gridColumn.FieldName, TotalFormat) });
if (SystemInfo.Instance.GroupSpecialMode)
{
//gridView.GroupSummary.Add(new GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Count, gridColumn.FieldName, null, "合计: {0:#,###}行"));
gridView.OptionsView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways;
}
else
{
gridView.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Count, gridColumn.FieldName, null, TotalFormat) });
}
isFirstVisableCol = false;
}
else
{
if (model.CanSum)
{
gridColumn.Summary.AddRange(new GridSummaryItem[] { new GridColumnSummaryItem((!string.IsNullOrEmpty(model.sumCalc) || !string.IsNullOrEmpty(model.SumCond) || model.isSumMerge) ? SummaryItemType.Custom : SummaryItemType.Sum, gridColumn.FieldName, model.SumText + "{0:" + model.DataFormat + "}") });
if (SystemInfo.Instance.GroupSpecialMode)
{
gridView.GroupSummary.Add(new GridGroupSummaryItem((!string.IsNullOrEmpty(model.sumCalc) || !string.IsNullOrEmpty(model.SumCond) || model.isSumMerge) ? SummaryItemType.Custom : SummaryItemType.Sum, gridColumn.FieldName, gridColumn, model.SumText + "{0:" + model.DataFormat + "}"));
gridView.OptionsView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways;
}
else
{
gridView.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem((!string.IsNullOrEmpty(model.sumCalc) || !string.IsNullOrEmpty(model.SumCond) || model.isSumMerge) ? SummaryItemType.Custom : SummaryItemType.Sum, gridColumn.FieldName, null, model.SumText + "{0:" + model.DataFormat + "}") });
}
}
}
}
///
/// 行转列多表头列合计。动态列创建在独立的BandedGridView里,不能直接复用主表格的SetColumnSumming。
///
private void SetDynamicBandedColumnSumming(BandedGridView view, BandedGridColumn gridColumn, GridColumnModel model, ref bool isFirstVisibleColumn, bool useSimpleSum)
{
if (view == null || gridColumn == null || model == null || !gridColumn.Visible) return;
if (isFirstVisibleColumn)
{
view.OptionsView.ShowFooter = true;
gridColumn.Summary.AddRange(new GridSummaryItem[] { new GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count, gridColumn.FieldName, TotalFormat) });
if (SystemInfo.Instance.GroupSpecialMode)
{
view.OptionsView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways;
}
else
{
view.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(DevExpress.Data.SummaryItemType.Count, gridColumn.FieldName, null, TotalFormat) });
}
isFirstVisibleColumn = false;
return;
}
if (!model.CanSum) return;
SummaryItemType summaryType = useSimpleSum ? SummaryItemType.Sum : ((!string.IsNullOrEmpty(model.sumCalc) || !string.IsNullOrEmpty(model.SumCond) || model.isSumMerge) ? SummaryItemType.Custom : SummaryItemType.Sum);
string format = model.SumText + "{0:" + model.DataFormat + "}";
view.OptionsView.ShowFooter = true;
gridColumn.Summary.AddRange(new GridSummaryItem[] { new GridColumnSummaryItem(summaryType, gridColumn.FieldName, format) });
if (SystemInfo.Instance.GroupSpecialMode)
{
view.GroupSummary.Add(new GridGroupSummaryItem(summaryType, gridColumn.FieldName, gridColumn, format));
view.OptionsView.GroupFooterShowMode = GroupFooterShowMode.VisibleAlways;
}
else
{
view.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(summaryType, gridColumn.FieldName, null, format) });
}
}
///
/// 行转列树形多表头列合计。TreeList使用SummaryFooter,需单独设置。
///
private void SetDynamicTreeColumnSumming(DevExpress.XtraTreeList.TreeList treeList, DevExpress.XtraTreeList.Columns.TreeListColumn gridColumn, GridColumnModel model, ref bool isFirstVisibleColumn)
{
if (treeList == null || gridColumn == null || model == null || !gridColumn.Visible) return;
if (isFirstVisibleColumn)
{
treeList.OptionsView.ShowSummaryFooter = true;
gridColumn.AllNodesSummary = true;
gridColumn.SummaryFooter = DevExpress.XtraTreeList.SummaryItemType.Count;
gridColumn.RowFooterSummaryStrFormat = TotalFormat;
isFirstVisibleColumn = false;
return;
}
if (!model.CanSum) return;
treeList.OptionsView.ShowSummaryFooter = true;
gridColumn.AllNodesSummary = true;
gridColumn.SummaryFooter = DevExpress.XtraTreeList.SummaryItemType.Sum;
gridColumn.RowFooterSummaryStrFormat = model.SumText + "{0:" + model.DataFormat + "}";
}
///
/// 说明:创建只读列
/// 创建人:龚宇超
/// 创建日期:2017-08-21
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The table.
/// 需要显示表格列右键菜单(保存列属性、重置列属性)则传递此值.
public virtual void SetReadOnlyColumns(DataTable table, string customColumKey = "")
{
Dictionary