using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraGrid.Views.Grid; using System.Data; using Lskj.Model; using System.Windows.Forms; using DevExpress.XtraGrid.Columns; using Lskj.Control.Properties; using Lskj.Util; using Lskj.Data; using DevExpress.XtraGrid.Views.Base; using Lskj.Business.Impl; namespace Lskj.Control.Model { /// /// GridView右键菜单 /// public class GridViewMenuStrip : BaseRightMenu { private int[] _oldSelectRows; private DataRow _oldDataRow; private int _oldRowHandler; private GridView _gridView; private MyControl _control; protected GridControlEx GridControlObj; public override void InitRightMenus(System.Windows.Forms.Control view, DataTable table, DynamicModel model, MyControl control = null, ContextMenuStrip menu = null) { base.InitRightMenus(view, table, model, control, menu); this.GridControlObj = view as GridControlEx; if (this.GridControlObj != null) { this.BaseGridView = this._gridView = this.GridControlObj.GridView; this._control = control; this.GridControlObj.gridControl.ContextMenuStrip = base.MenuStrip; } } protected override void MenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e) { int[] mSelectRows = this._gridView.GetSelectedRows() ?? new int[0]; DataRow[] dataRows = new DataRow[mSelectRows.Length]; for (int i = 0; i < mSelectRows.Length; i++) { dataRows[i] = this._gridView.GetDataRow(mSelectRows[i]); } ContextMenuStrip cmsMenu = (ContextMenuStrip)sender; cmsMenu.Items.Clear(); cmsMenu.Items.AddRange(this.RightMenuItems.ToArray()); if (mSelectRows.Length > 0) { DataRow mSelectRow = this._gridView.GetDataRow(mSelectRows[0]); this._gridView.UpdateCurrentRow(); this._gridView.ShowEditor(); // 创建QQ、浏览器右键菜单 List mQQArray = new List(); List mBrowerArray = new List(); foreach (GridColumn column in this._gridView.Columns) { if (!mSelectRow.Table.Columns.Contains(column.Name)) continue; string rowValue = mSelectRow[column.Name] + ""; if (!string.IsNullOrWhiteSpace(rowValue)) { GridColumnModel model = column.Tag as GridColumnModel; if (model == null) continue; if (model.FieldType == ControlType.LabQQ) { ToolStripMenuItem item = new ToolStripMenuItem("(" + column.Caption + ")" + rowValue, Resources.qq); item.Tag = mSelectRow[column.Name] + ""; item.Click += new EventHandler(QQItemClick); mQQArray.Add(item); } if (model.FieldType == ControlType.LabWWW && !string.IsNullOrWhiteSpace(rowValue)) { ToolStripMenuItem item = new ToolStripMenuItem("(" + column.Caption + ")" + rowValue, Resources.www); item.Tag = mSelectRow[column.Name] + ""; item.Click += new EventHandler(BrowerItemClick); mBrowerArray.Add(item); } } } cmsMenu.Items.AddRange(mQQArray.ToArray()); cmsMenu.Items.AddRange(mBrowerArray.ToArray()); } Dictionary cmsModelDic = new Dictionary(); List roleNamesList = new List(); foreach (ToolStripItem cms in cmsMenu.Items) { if (cms.Tag is GridRightMenuModel model) { cmsModelDic[cms] = model; roleNamesList.Add(model.PrivilegeOper); } } // 判断右键菜单是否可用 foreach (ToolStripItem cms in cmsMenu.Items) { GridRightMenuModel model = null; if (cmsModelDic.ContainsKey(cms)) { model = cmsModelDic[cms]; } else if (cms.Tag is GridRightMenuModel gridRightMenuModel) { model = gridRightMenuModel; } if (model != null) { if (!string.IsNullOrWhiteSpace(model.MenuCond)) { try { bool condResult = false; StringBuilder condBuilder = new StringBuilder(); StringBuilder sqlCondBuilder = new StringBuilder(); GridCell[] cells = this._gridView.GetSelectedCells(); DataRow focusedRow = this.BaseGridView.GetFocusedDataRow(); DataRow[] conditionRows = dataRows; if (model.AllowNullExec && conditionRows.Length == 0) { conditionRows = new DataRow[] { new DataTable().NewRow() }; } if (conditionRows.Length == 0) continue; foreach (DataRow selectRow in conditionRows) { //DataRow selectRow = this._gridView.GetDataRow(itemIndex); string cond = ReplaceHelper.ReplaceUserInfo(model.MenuCond); cond = RaiseBeforeHandleParamsCallback(model.ParamList, conditionRows, model.MenuCond, selectRow, cond); if (this._control != null) { cond = _control.ReplaceParentControlValue(cond); } //cond = ReplaceHelper.ReplaceRowParam(selectRow, cond); cond = ReplaceHelper.ReplaceRowParamEmptyWrapQuote(selectRow, cond); if (model.Mergeexec&& cond.StartsWith("@")) { //替换[],替换的是多选行中的数据 cond = ReplaceHelper.ReplaceRowParam(conditionRows, cond.Replace("[", "{").Replace("]", "}"), "", ""); } if (this._gridView != null) { if (cells != null && cells.Length > 0 && cond.Contains("{COLUMN_")) { GridCell cell = cells[0]; string colValue = focusedRow == null || !focusedRow.Table.Columns.Contains(cell.Column.Name) ? "" : focusedRow[cell.Column.Name] + ""; cond = cond.ReplaceColumnParam(cell.Column.Name, cell.Column.Caption, colValue); } } if (ControlObj != null) { cond = ControlObj.ReplaceParentControlValue(cond); } if (cond.StartsWith("@")) { sqlCondBuilder.Append($"({cond.TrimStart('@')}) = '1' and "); } else { condBuilder.Append($"({cond}) and "); } } string condStr = model.MenuCond; if (!string.IsNullOrWhiteSpace(sqlCondBuilder.ToString()) || !string.IsNullOrWhiteSpace(condBuilder.ToString())) { if (model.MenuCond.StartsWith("@")) { condStr = $"@if({sqlCondBuilder.ToString().TrimEnd().TrimEnd('d', 'n', 'a')}) begin select '1' end else begin select '0' end"; } else { condStr = condBuilder.ToString().TrimEnd().TrimEnd('d', 'n', 'a'); } } condResult = ValidateCond(condStr, null); //if (!condResult) break; //cms.Enabled = ReplaceHelper.EvalCond(cond); if (model.ForbiddenDisplay) { cms.Visible = condResult; } cms.Enabled = condResult; } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(ResourceKeys.SetRightMenuCondFault + "\r\n" + Message); e.Cancel = true; break; } } } } } protected override void MenuStripItemClick(object sender, EventArgs e) { base.MenuStripItemClick(sender, e); } protected override DataRow[] GetSelectedRows() { return this.GridControlObj.GetViewFocusedDataRows(); } protected override DataTable GetDataTable() { return this.GridControlObj.GridControl.DataSourceTable(); } protected override void MenuStripClickBefore(ToolStripMenuItem item, GridRightMenuModel model) { this._oldSelectRows = this._gridView.GetSelectedRows(); this._oldDataRow = this._gridView.GetFocusedDataRow(); this._oldRowHandler = this._gridView.FocusedRowHandle; } protected override void MenuStripClickAfter(ToolStripMenuItem item, GridRightMenuModel model) { DataTable dataTable = this.GridControlObj.GridView.GetGridViewFilteredAndSortedDataToDataTable() as DataTable; List rows = new List() { this._oldDataRow }; IEnumerable QBJ = dataTable.Select().AsEnumerable().Intersect(rows.AsEnumerable(), DataRowComparer.Default); int NRowHandle = 0; try { DataTable NDataTable = QBJ.CopyToDataTable(); if (NDataTable.Rows.Count > 0) { NRowHandle = dataTable.Rows.IndexOf(QBJ.First()); this._oldRowHandler = NRowHandle >= 0 ? NRowHandle : this._oldRowHandler; } } catch (System.InvalidOperationException ex) //如果差集为null,就回进当前报错 { } if (this._gridView.OptionsSelection.MultiSelect == true && this._gridView.OptionsSelection.EnableAppearanceFocusedCell == false && this._gridView.OptionsSelection.MultiSelectMode == GridMultiSelectMode.CheckBoxRowSelect) { if (model.RefreshCheckState) this._gridView.ClearSelection(); else if (!model.RefreshCheckState && this._oldSelectRows != null) { foreach (int index in this._oldSelectRows) { this._gridView.SelectRow(index); } } } else { this._gridView.ClearSelection(); this._gridView.SelectRowHandler(this._oldRowHandler); } } protected override List HandleRightMenuParams(List paramList, DataRow rowData, DataRow[] rowDatas = null, int actionType = 0) { List handleParamList = base.HandleRightMenuParams(paramList, rowData, rowDatas, actionType); foreach (string item in handleParamList) { if (item.StartsWith("{")) { string field = item.Replace("{", "").Replace("}", ""); if ("COLUMN_NAME".Equals(field)) { handleParamList.Add(this._gridView.FocusedColumn.FieldName); } else if ("COLUMN_TITLE".Equals(field)) { handleParamList.Add(this._gridView.FocusedColumn.GetTextCaption()); } else { handleParamList.Add(rowData[field] + ""); } } } return handleParamList; } } }