public GridOilCellGroup GetGridOilCellGroup(string rowItemCode, int columnIndex) { if (string.IsNullOrWhiteSpace(rowItemCode) || _rows == null || _cols == null || _rows.Count == 0 || _cols.Count == 0 || columnIndex < 0 || columnIndex >= _cols.Count) { return(null); } var row = _rows.FirstOrDefault(o => o.itemCode == rowItemCode); if (row == null) { return(null); } int rowIndex = _rows.IndexOf(row); var column = columnList[columnIndex]; var col = _cols[columnIndex]; var cell = this[column.LabColumn.Index, rowIndex] as GridOilCellItem; GridOilCellGroup cellGroup = null; if (cell != null) { cellGroup = cell.Group; } else { cellGroup = new GridOilCellGroup(column, _rows.IndexOf(row)); cellGroup.Bind(); } return(cellGroup); }
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e) { var col = Columns[e.ColumnIndex] as GridOilColumnItem; if (col == null) { e.Cancel = true; return; } base.OnCellBeginEdit(e); var cell = this[e.ColumnIndex, e.RowIndex] as GridOilCellItem; if (cell == null) { GridOilCellGroup g = new GridOilCellGroup(col.Group, e.RowIndex, null); g.Bind(); switch (col.Type) { case GridOilColumnType.Lab: cell = g.LabCell; break; case GridOilColumnType.Calc: cell = g.CalcCell; break; default: e.Cancel = true; return; } } cell.Value = cell.Value2; }
/// <summary> /// 撤销修改值 /// </summary> /// <param name="dgv"></param> private void UndoRedoValues(DataGridViewUndoRedoManager manager, bool isUndo) { if (manager == null) { return; } var dgv = manager.GridView; if (dgv == null || Values == null || Values.Count == 0) { return; } foreach (var column in Values.GroupBy(o => o.ColumnIndex)) { Application.DoEvents(); var col = dgv.Columns[column.Key]; var col2 = col as GridOilColumnItem; if (col2 == null) { foreach (var cell in column) { dgv[cell.ColumnIndex, cell.RowIndex].Value = isUndo ? cell.OldValue : cell.NewValue; } } else { foreach (var e in column) { var cell = dgv[e.ColumnIndex, e.RowIndex] as GridOilCellItem; GridOilCellGroup cellGroup; if (cell == null) { cellGroup = new GridOilCellGroup(col2.Group, e.RowIndex, null); cellGroup.Bind(); switch (col2.Type) { case GridOilColumnType.Lab: cell = cellGroup.LabCell; break; case GridOilColumnType.Calc: cell = cellGroup.CalcCell; break; default: continue; } } cell.Value2 = isUndo ? e.OldValue : e.NewValue; } } } }
/// <summary> /// 设置值 /// </summary> /// <param name="data"></param> /// <param name="updateTypes"></param> public void SetData(List <OilDataEntity> data, GridOilColumnType updateTypes = GridOilColumnType.Calc, bool enableUndoRedo = true) { if (_rows == null || _cols == null || _rows.Count == 0 || _cols.Count == 0 || data == null || updateTypes == GridOilColumnType.None) { return; } List <DataGridViewUndoRedoValue> updatedValues = new List <DataGridViewUndoRedoValue>(); foreach (var c in data.GroupBy(o => o.ColumnIndex)) { var col = columnList[c.Key]; foreach (var r in c) { var cell = this[col.LabColumn.Index, r.RowIndex] as GridOilCellItem; GridOilCellGroup cellGroup; if (cell == null) { cellGroup = new GridOilCellGroup(col, r.RowIndex); cellGroup.Bind(); } else { cellGroup = cell.Group; } string oldValue; if (updateTypes.HasFlag(GridOilColumnType.Calc)) { cell = cellGroup.CalcCell; oldValue = cell.Value2; cell.Value2 = r.calData; if (enableUndoRedo) { updatedValues.Add(new DataGridViewUndoRedoValue() { NewValue = cell.Value2, OldValue = oldValue, ColumnIndex = cell.ColumnIndex, RowIndex = cell.RowIndex }); } } if (updateTypes.HasFlag(GridOilColumnType.Lab)) { cell = cellGroup.LabCell; oldValue = cell.Value2; cell.Value2 = r.labData; if (enableUndoRedo) { updatedValues.Add(new DataGridViewUndoRedoValue() { NewValue = cell.Value2, OldValue = oldValue, ColumnIndex = cell.ColumnIndex, RowIndex = cell.RowIndex }); } } } Application.DoEvents(); } if (enableUndoRedo && updatedValues.Count > 0) { undoRedoManager.DoChangeValues(updatedValues); } }
private bool PasteClipboard() { if (Clipboard.ContainsText() == false) { MessageBox.Show("粘贴内数据不正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } var error = columnList.Count == 0 || SelectedCells.Count == 0; if (error == false) { foreach (DataGridViewColumn c in SelectedColumns) { if (c is GridOilColumnItem == false) { error = true; break; } } } var t = columnList.Min(o => o.LabColumn.Index); if (error == false) { foreach (DataGridViewCell c in SelectedCells) { if (c.ColumnIndex < t || c.ReadOnly) { error = true; break; } } } var selectRange = error ? null : GetSelectedCellsRange(); if (error || selectRange == null) { MessageBox.Show("粘贴区域不正确。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (SelectedCells.Count != selectRange.Value.Height * selectRange.Value.Width) { MessageBox.Show("不能粘贴多重选区域。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } List <DataGridViewUndoRedoValue> updatedValues = new List <DataGridViewUndoRedoValue>(); IsBusy = true; try { string s = Clipboard.GetText(); string[] lines = s.Split('\n'); int maxColumnIndex = 0, maxRowIndex = 0; int minRowIndex = selectRange.Value.Y; int minColumnIndex = selectRange.Value.X; var isSelectedOne = SelectedCells.Count == 1; if (isSelectedOne) { maxColumnIndex = this.ColumnCount; maxRowIndex = this.RowCount; } else { maxColumnIndex = selectRange.Value.Right; maxRowIndex = selectRange.Value.Bottom; } foreach (string line in lines) { Application.DoEvents(); int skipHideColumn = 0; if (minRowIndex < maxRowIndex && line.Length > 0) { string[] sCells = line.Split('\t'); for (int i = 0; i < sCells.Length; ++i) { int columnIndex = minColumnIndex + i + skipHideColumn; if (columnIndex >= maxColumnIndex) { break; } if (Columns[columnIndex].Visible == false) { skipHideColumn++; i--; continue; } var cell = this[columnIndex, minRowIndex] as GridOilCellItem; if (cell == null) { var col = Columns[columnIndex] as GridOilColumnItem; var cellGroup = new GridOilCellGroup(col.Group, minRowIndex, null); cellGroup.Bind(); switch (col.Type) { case GridOilColumnType.Lab: cell = cellGroup.LabCell; break; case GridOilColumnType.Calc: cell = cellGroup.CalcCell; break; default: return(false); } } var oldValue = cell.Value2; if (sCells[i].EndsWith("\r")) { cell.Value2 = sCells[i].Replace("\r", ""); } else { cell.Value2 = sCells[i]; } if (isSelectedOne) { cell.Selected = true; } updatedValues.Add(new DataGridViewUndoRedoValue() { NewValue = cell.Value2, OldValue = oldValue, ColumnIndex = cell.ColumnIndex, RowIndex = cell.RowIndex }); } minRowIndex++; } else { break; } } } catch (FormatException ex) { MessageBox.Show("粘贴错误。\n" + ex.Message); return(false); } finally { IsBusy = false; } if (updatedValues.Count > 0) { undoRedoManager.DoChangeValues(updatedValues); } needSave = true; return(true); }
protected override void OnCellEndEdit(DataGridViewCellEventArgs e) { // base.OnCellEndEdit(e); var col = Columns[e.ColumnIndex] as GridOilColumnItem; if (col == null) { return; } var newValue = this[e.ColumnIndex, e.RowIndex].Value as string; //根据行列号获取单元格的值 var cell = this[e.ColumnIndex, e.RowIndex] as GridOilCellItem; GridOilCellGroup cellGroup; if (cell != null) { cellGroup = cell.Group; } else { cellGroup = new GridOilCellGroup(col.Group, e.RowIndex, null); cellGroup.Bind(); switch (col.Type) { case GridOilColumnType.Lab: cell = cellGroup.LabCell; break; case GridOilColumnType.Calc: cell = cellGroup.CalcCell; break; default: return; } } var oldValue = cell.Value2; //if (newValue == oldValue) //与原始值一样,不用更新 // return; cell.Value2 = newValue as string; #region 因为两个单元格为一个实体,如果修改单元格,相关联的单元格是否有实体存在,若有则属于更新实体,相关联的两个单元格都没实体时才属于插入 if (col.Type == GridOilColumnType.Lab) { undoRedoManager.DoChangeValue(cell.ColumnIndex, cell.RowIndex, oldValue, cell.Value2); #region var cellCal = cellGroup.CalcCell; //获取校正单元 //单元格值为“-”,或原来的单元格为”-“, 校正值不表,否则修改校正单元格的值 if (!(Convert.ToString(newValue) == "-" || oldValue == "-" || oldValue == "-")) { if (AutoReplenished && string.IsNullOrWhiteSpace(cellCal.Value2)) { oldValue = cellCal.Value2; cellCal.Value2 = Convert.ToString(newValue); //联动,实体的calData值也等于新值 undoRedoManager.DoChangeValue(cellCal.ColumnIndex, cellCal.RowIndex, oldValue, cellCal.Value2); } } #endregion } else //如果当前是校正单元格,先判断左边实测单元格是否为null若不为nuu,则校正单元格的cellValue实体=左边实测单元格cellValue实体 { if (string.IsNullOrWhiteSpace(newValue as string) == false) { var s = (newValue as string).Trim(); if (s == "-" || s == "-") { cell.Value2 = cellGroup.LabCell.Value2; } } undoRedoManager.DoChangeValue(cell.ColumnIndex, cell.RowIndex, oldValue, cell.Value2); } #endregion SetRemarkFlag(false); needSave = true; }
private Color _color = Color.Red; //设置检查和审查的颜色 public GridOilCellItem(GridOilCellGroup group, GridOilColumnType type) { Group = group; Type = type; MaxInputLength = 20; }