using DevExpress.XtraEditors; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Grid; using Lskj.Business; using Lskj.Business.Impl; using Lskj.Control; using Lskj.Control.Model; using Lskj.Data; using Lskj.Model; using Lskj.Util; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Lskj.PubBillManagement { public partial class FrmDocumentSource : BaseForm { /// /// 配置的关联ModuleCodel值 /// /// The Union ModuleCodel. public string UnionModuleCodel; /// /// 左侧树数据 /// public DataRow LeftRowObj; /// /// 系统模块实体对象 /// /// The system model. public ModuleModel SysModel { get; private set; } /// /// 自定义查询条件 /// public MyControl SearchObj { get; private set; } /// /// 第一次进入 /// private bool isFirst = true; /// /// 第一次进入 /// private bool isFirstSearch = true; /// /// 左侧树结构 /// public TreeViewEx LeftTreeViewEx; /// /// 左侧表格 /// public GridControlEx LeftGridEx; /// /// 左侧结构关联字段 /// public string ParentUnionField; /// /// 左侧树结构、表格主键 /// public string ParentKeyField; /// /// CurrentOperColumnKey值(保存表格属性会用) /// /// The data source. public string CurrentOperColumnKey = string.Empty; /// /// 左侧表格查询条件 /// private MyControl _leftGridSearchObj; /// /// 返回行 /// public DataRow[] DataRows = null; public FrmDocumentSource(string ModuleCodel) { InitializeComponent(); this.UnionModuleCodel = ModuleCodel; this.Disposed += new EventHandler(OnFrmModelLookUp_Disposed); this.gcMain.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDown; DataRow modelRow = MainImpl.GetSystemdllTab(this.UnionModuleCodel);//获取单个模块信息 if (modelRow == null) { MessageUtil.Show("模块编号[" + UnionModuleCodel + "],配置错误!\r\n该模块信息未找到!"); return; } this.SysModel = new ModuleModel(modelRow);// 系统模块实体对象 //设置关联模块(左侧) this.InitLeftControls(); //设置主表 this.InitMainControls(); this.btnCancel.Click += btnCancel_Click; this.btnOk.Click += btnOk_Click; } /// /// 初始化左侧表 /// public void InitLeftControls() { if (this.SysModel != null) { switch (this.SysModel.MenuType) { case 1: // 左侧为树节点 this.pl_left_top.Visible = false; this.gridLeft.Visible = false; this.treeLeft.Visible = true; this.InitlizeSpiltLocation();//设置分割条位置 this.SetLeftTreeView();//设置左侧树数据 this.LeftTreeViewEx = this.treeLeft; break; case 2: // 左侧为表格 this.gridLeft.Visible = true; this.treeLeft.Visible = false; this.InitlizeSpiltLocation(); this.SetLeftGridView(); this.LeftGridEx = this.gridLeft; break; case 3: // 左侧为空 default: this.splitMain.PanelVisibility = SplitPanelVisibility.Panel2; break; } } else { this.splitMain.PanelVisibility = SplitPanelVisibility.Panel2; } } /// /// 初始化主表 /// public void InitMainControls() { //设置上方条件 this.pl_top.Controls.Clear(); DataTable queryTable = BaseModuleImpl.GetCustomQueryFields(this.SysModel.CondKey);//加载自定义配置字段 this.SearchObj = new MyControl(this.SysModel.MenuSql, this.gcMain, LeftTreeViewEx, LeftGridEx); this.SearchObj.ParentKeyField = this.ParentKeyField; this.SearchObj.ParentUnionField = this.ParentUnionField; this.SearchObj.OnDataSourceBindCallBack += new EventHandler(OnFirstPageDataSourceBindCallBack); this.SearchObj.OnSearchAfterCallBack += new EventHandler(OnSearchAfterd); this.pl_top.Height = 0; this.pl_top.Height = this.SearchObj.InitSearchControl(queryTable, this.pl_top); //创建表格列 DataTable _gridColumns = BaseModuleImpl.GetBaseGridColumns(this.UnionModuleCodel); this.gcMain.SetReadOnlyColumns(_gridColumns, GridCustomColumnStruct.BaseMainGridView + this.SysModel.FormKey); //this.SetCustomColumns(); //设置复选框 GridDragGrid.GridAddCheckBox(gcMain.GridView); //右键 DataTable dt = BaseModuleImpl.GetBaseGridRightMenus(this.UnionModuleCodel); string[] obj = new string[]{ "", ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, "3", this.UnionModuleCodel, }; ; DynamicModel dynamicModel = new DynamicModel(obj); this.gcMain.SetGridRightMenus(dt, dynamicModel, this.OnGridViewRightCallBack, this.SearchObj, null); //this.gcMain.GridView.OptionsView.ColumnAutoWidth = true; } /// /// 查询后清空复选框勾选 /// /// /// private void OnSearchAfterd(object sender, EventArgs e) { this.gcMain.GridView.ClearSelection(); } /// /// 初始化后默认查询 /// /// /// private void OnFirstPageDataSourceBindCallBack(object sender, EventArgs e) { //打开时默认选中第一条 if (LeftTreeViewEx != null && LeftTreeViewEx.TreeView.Nodes.Count > 0) { LeftTreeViewEx.TreeView.SelectedNode = LeftTreeViewEx.TreeView.Nodes[0]; LeftTreeViewEx.TreeView.Nodes[0].Expand(); } else if (LeftGridEx != null) { LeftGridEx.GridView.SelectRow(0); } this.SearchObj.SearchGrid(); } #region 设置分割条位置 /// /// 说明:设置分割条位置 /// 创建人:王一帆 /// 创建日期:2021-07-21 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// protected void InitlizeSpiltLocation() { try { string width = IniHelper.Read(string.Format("bill_Special_Width_{0}", this.UnionModuleCodel));//通过Key获取Value值 if (!string.IsNullOrEmpty(width)) { this.splitMain.SplitterPosition = Convert.ToInt32(width); } } catch (Exception ex) { LogHelper.Instance.WriteError(ex); MessageUtil.Show(ex); } } #endregion #region 保存表格属性 /// /// 说明:保存表格属性 /// 创建人:王一帆 /// 创建日期:2021-07-20 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. protected virtual void SaveItemClick(object sender, EventArgs e) { DataRow rowItem = null; bool success = BaseImpl.HasExistsDataRow(ResourceKeys.SettingTableName, "formKey", CurrentOperColumnKey, string.Format(" and OperatorId='{0}' ", ERPInfo.Instance.UserId), out rowItem); if (!success) { MessageUtil.Show(ResourceKeys.NotFoundSettingColumnTable); return; } if (MessageUtil.Show(ResourceKeys.IsSaveColumn, MessageBoxButtons.YesNo) == DialogResult.Yes) { try { if (rowItem == null) { success = this.gcMain.GridView.SaveCustomColumnToDatabase(this.CurrentOperColumnKey); } else { success = this.gcMain.GridView.UpdateCustomColumnToDatabase(this.CurrentOperColumnKey); } MessageUtil.Show(success ? ResourceKeys.SaveSuccess : ResourceKeys.SaveFault); } catch (SqlException sex) { if (sex.Message.Contains(ResourceKeys.FieldLenIsShort) && MessageUtil.Show(ResourceKeys.SaveFault + "\r\n" + ResourceKeys.CanUpgradeTable, MessageBoxButtons.YesNo) == DialogResult.Yes && BaseImpl.UpdateTableField(ResourceKeys.SettingTableName, "formKey", "varchar(200)")) { MessageUtil.Show(ResourceKeys.UpdateSuccess); } } catch (Exception ex) { LogHelper.Instance.WriteError(ex); LogUtil.WriteError("保存表格属性出错", ex); MessageUtil.Show(ex); } } } #endregion #region 加载自定义列 /// /// 说明:加载自定义列 /// 创建人:王一帆 /// 创建日期:2019-9-16 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetCustomColumns() { if (string.IsNullOrEmpty(this.CurrentOperColumnKey) || !BaseImpl.HasExistsTable(ResourceKeys.SettingTableName)) return; DataTable customTable = this.gcMain.GridView.GetCustomColumnByDatabase(this.CurrentOperColumnKey); if (customTable != null && customTable.Rows.Count > 0) { foreach (DataRow rowItem in customTable.Rows) { string fieldName = rowItem["fieldName"] + ""; int fieldWidth = Convert.ToInt32(rowItem["fieldWidth"]); int index = Convert.ToInt32(rowItem["orderid"]); bool visible = "1".Equals(rowItem["isVisible"] + "") && fieldWidth > 0; bool isFix = "True".Equals(rowItem["IfFixColumn"] + "", StringComparison.OrdinalIgnoreCase); GridColumn gridColumn = this.gcMain.GridView.Columns.ColumnByName(fieldName); if (gridColumn != null && gridColumn.Visible && gridColumn.Width > 0) { GridColumn colObj = this.gcMain.GridView.Columns[fieldName]; colObj.VisibleIndex = index; colObj.Width = fieldWidth; colObj.Visible = visible; colObj.Fixed = isFix ? FixedStyle.Left : FixedStyle.None; } } } } #endregion #region 刷新左侧树数据 /// /// 说明:设置左侧树数据 /// 创建人:王一帆 /// 创建日期:2021-07-21 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// protected void SetLeftTreeView() { this.LeftRowObj = BaseModuleImpl.GetBaseLeftTreeField(this.UnionModuleCodel);//获取表格左侧树字段 if (this.LeftRowObj != null) { //this.ModuleGridObj.ParentUnionField = this.LeftRowObj["fieldname"] + ""; this.ParentUnionField = this.LeftRowObj["fieldname"] + ""; this.ParentKeyField = this.LeftRowObj["fieldsqlid"] + ""; this.treeLeft.TreeNodeKeyField = this.LeftRowObj["fieldsqlid"] + ""; this.treeLeft.TreeNodeTextField = this.LeftRowObj["fieldsqlname"] + ""; this.treeLeft.TreeNodeSelectAfter += new TreeViewEventHandler(OnTreeLeftTreeNodeSelected); this.treeLeft.BindTreeView(BaseImpl.GetDataTableResult(this.LeftRowObj["fieldsql"] + "")); } else { // 根据模块上配置的sql加载树 if (this.SysModel != null && !string.IsNullOrEmpty(this.SysModel.TreeSQL)) { this.ParentUnionField = "SpeciesNo"; this.ParentKeyField = "SpeciesNo"; this.treeLeft.TreeNodeKeyField = "SpeciesNo"; this.treeLeft.TreeNodeTextField = "SpeciesName"; this.treeLeft.TreeNodeSelectAfter += new TreeViewEventHandler(OnTreeLeftTreeNodeSelected); this.treeLeft.BindTreeView(BaseImpl.GetDataTableResult(this.SysModel.TreeSQL)); } } } /// /// 说明:树节点点击后刷新数据 /// 创建人:龚宇超 /// 创建日期:2017-10-26 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void OnTreeLeftTreeNodeSelected(object sender, TreeViewEventArgs e) { string where = string.Format(" and {0} like '{1}%' ", this.treeLeft.TreeNodeKeyField, e.Node.Name); string sqlValue = ReplaceHelper.IsProcedure(this.SysModel.MenuSql) ? ReplaceHelper.ReplaceTreeViewParentKeyCond(this.SysModel.MenuSql, treeLeft.GetSelectNodeValue()) : ReplaceHelper.ReplaceWhereCond(this.SysModel.MenuSql) + where; //this.gcMain.gridControl.DataSource = MainImpl.GetDataTableResult(sqlValue); this.SearchObj.SearchGrid(sqlValue); this.gcMain.GridView.ClearSelection(); } #region 设置左侧表格数据 /// /// 说明:设置左侧表格数据 /// 创建人:龚宇超 /// 创建日期:2017-10-25 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// protected void SetLeftGridView() { this.LeftRowObj = BaseModuleImpl.GetBaseLeftTreeField(UnionModuleCodel);//获取表格左侧树字段 if (this.LeftRowObj != null) { DataTable dtTable = BaseModuleImpl.GetBaseLeftGridField(UnionModuleCodel); DataTable dtColorTable = BaseModuleImpl.GetBaseGridRowColors(this.LeftRowObj["fieldKey"] + ""); this.gridLeft.GridView.RowClick += new DevExpress.XtraGrid.Views.Grid.RowClickEventHandler(OnGridViewRowClick); this.gridLeft.SetGridRowColors(dtColorTable); this.gridLeft.SetReadOnlyColumns(dtTable, GridCustomColumnStruct.BaseLeftGridView + UnionModuleCodel); this.ParentUnionField = this.LeftRowObj["fieldname"] + ""; this.ParentKeyField = this.LeftRowObj["fieldsqlid"] + ""; //this.ModuleGridObj.ParentKeyField = this.LeftRowObj["fieldsqlid"] + ""; //this.ModuleGridObj.ParentUnionField = this.LeftRowObj["fieldname"] + ""; DataTable table = BaseModuleImpl.GetCustomQueryFields(this.LeftRowObj["fieldKey"] + ""); string searchSql = this.LeftRowObj["fieldsql"] + ""; if (table != null && table.Rows.Count > 0) { // 自定义查询条件 this._leftGridSearchObj = new MyControl(searchSql, this.gridLeft); this.pl_left_top.Height = this._leftGridSearchObj.InitSearchControl(table, this.pl_left_top); this._leftGridSearchObj.OnDataSourceBindCallBack += new EventHandler(OnLeftControlObjDataSourceBindCallBack); } else { DataTable leftGridCondtionTable = BaseImpl.GetDataTableResult(searchSql); this.pl_left_top.Visible = false; this.gridLeft.GridControl.DataSource = leftGridCondtionTable; } } } #endregion #region 刷新右侧表格数据 /// /// 说明:刷新右侧表格数据 /// 创建人:龚宇超 /// 创建日期:2018-03-19 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void OnGridViewRowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { try { string sqlValue = ReplaceHelper.ReplaceWhereCond(this.SysModel.MenuSql); if (this.gridLeft.Visible) { DataRow rowitem = this.gridLeft.GridView.GetFocusedDataRow(); sqlValue = this._leftGridSearchObj != null ? this._leftGridSearchObj.ReplaceParentControlValue(sqlValue) : sqlValue; sqlValue = ReplaceHelper.ReplaceRowParam(rowitem, sqlValue); } this.SearchObj.SearchGrid(sqlValue); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } #endregion #endregion #region 事件 /// /// 说明:数据绑定完成后加载数据 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void OnLeftControlObjDataSourceBindCallBack(object sender, EventArgs e) { try { this._leftGridSearchObj.SearchGrid(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } #region 确定 /// /// 说明:确定 /// 创建人:龚宇超 /// 创建日期:2018-03-20 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void btnOk_Click(object sender, EventArgs e) { this.DataRows = gcMain.GetViewFocusedDataRows(); if (DataRows.Length > 0) { this.DialogResult = System.Windows.Forms.DialogResult.OK; } this.Close(); } #endregion #region 取消 /// /// 说明:取消 /// 创建人:龚宇超 /// 创建日期:2018-03-20 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } #endregion #region 释放时清理数据源 /// /// 说明:释放时清理数据源 /// 创建人:龚宇超 /// 创建日期:2018-03-16 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 private void OnFrmModelLookUp_Disposed(object sender, EventArgs e) { GC.Collect(); } #endregion #endregion /// /// 说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新 /// 创建人:龚宇超 /// 创建日期:2017-11-02 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. protected void OnGridViewRightCallBack(object sender, EventArgs e) { try { this.SearchObj.SearchLastGrid(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } } }