/// <summary> /// 插入行并添加Do事件到RedoUndo里 /// </summary> private void OnInsertRowToolStripMenuItem_MouseDown(object sender, MouseEventArgs e) { m_DataGridView.CellValueChanged -= OnDataGridView_CellValueChanged; if (!Initialized) { return; } int offset = 0; ToolStripMenuItem item = (ToolStripMenuItem)sender; if (item == m_InsertDownRowToolStripMenuItem) { offset = 1; } else if (item == m_InsertUpRowToolStripMenuItem) { offset = 0; } else { return; } if (m_DataGridView.SelectedRows.Count < 1) { return; } int index = m_DataGridView.SelectedRows[0].Index + offset; DataGridViewUtility.InsertNewRow(m_DataGridView, m_DataTable, index); m_DataGridView.ClearSelection(); m_DataGridView.Rows[index].Selected = true; m_CopyDataTable = m_DataTable.Copy(); EditManager.DidAddRow(index); OnDataGridViewData_Change(); UpdateGridHeader(); m_DataGridView.CellValueChanged += OnDataGridView_CellValueChanged; }
/// <summary> /// 插入行并添加Do事件到RedoUndo里 /// </summary> private void OnInsertNewRowToolStripMenuItem_MouseDown(object sender, MouseEventArgs e) { if (!Initialized) { return; } int offset = 0; ToolStripMenuItem item = (ToolStripMenuItem)sender; if (item == m_InsertNewRowToDownToolStripMenuItem) { offset = 1; } else if (item == m_InsertNewRowToUpToolStripMenuItem) { offset = 0; } else { return; } if (m_DataGridView.SelectedRows.Count < 1) { return; } BeforeChangeCellValue(); int index = m_DataGridView.SelectedRows[0].Index + offset; DataGridViewUtility.InsertNewRow(m_DataGridView, m_DataTable, index); m_DataGridView.ClearSelection(); m_DataGridView.Rows[index].Selected = true; EditManager.DidAddRow(index); UpdateGridHeader(); AfterChangeCellValue(); }
public void Redo(DataGridView dataGridView, DataTable dataTable) { DataGridViewUtility.InsertNewRow(dataGridView, dataTable, Row); }
/// <summary> /// UNDONE 这个方法是复制了PasetCells后修改的,应该想办法把这两个方法封装一下 /// </summary> public void InsertFromClipboard(DataGridView dataGridView, DataTable dataTable, int rowIndex) { if (!CanInsertFromClipboard()) { return; } if (!GetclipboardText(out string clipboardText)) { return; } List <IUndoRedo> didManyThings = new List <IUndoRedo>(); try { m_CSVForm.BeforeChangeCellValue(); string[] lines = Regex.Split(clipboardText.TrimEnd("\r\n".ToCharArray()), "\r\n"); // 当前行 int currentRow = rowIndex; // 当前列 int currentCol = 0; DataGridViewCell currentCell; for (int lineIdx = 0; lineIdx < lines.Length; lineIdx++) { // 插入行 DataGridViewUtility.InsertNewRow(dataGridView, dataTable, currentRow); DoAddRowEvent doAddRowEvent = new DoAddRowEvent { Row = currentRow }; didManyThings.Add(doAddRowEvent); string line = lines[lineIdx]; // 行超过表格限制,默认不添加新行 if (currentRow >= dataGridView.RowCount) { throw (new ArgumentOutOfRangeException(null, string.Format("插入数据({0})行的第({1})行到表中第({2})行失败\n表一共有({3})行", lines.Length, lineIdx + 1, currentRow + 1, dataGridView.RowCount))); } string[] cells = line.Split('\t'); List <CellValueChangeItem> cellChangeList = new List <CellValueChangeItem>(); for (int cellIdx = 0; cellIdx < cells.Length; ++cellIdx) { // 列超过表格限制,默认不添加新列 if (currentCol + cellIdx >= dataGridView.ColumnCount) { throw (new ArgumentOutOfRangeException(null, string.Format("插入数据({0})列的第({1})列到表中第({2})列失败\n表一共有({3})列", cells.Length, cellIdx + 1, ConvertUtility.NumberToLetter(currentCol + cellIdx + 1), ConvertUtility.NumberToLetter(dataGridView.ColumnCount)))); } currentCell = dataGridView.Rows[currentRow].Cells[currentCol + cellIdx]; string cell = cells[cellIdx]; if (currentCell.Value == null || currentCell.Value.ToString() != cell) { // 如果cell是多行数据,去除两侧的引号 if (cell.Contains("\n") && cell[0] == '"' && cell[cell.Length - 1] == '"') { cell = cell.Substring(1, cell.Length - 2); } CellValueChangeItem change = new CellValueChangeItem(); change.Row = currentCell.RowIndex; change.Column = currentCell.ColumnIndex; change.OldValue = (string)currentCell.Value; change.NewValue = cell; currentCell.Value = cell; cellChangeList.Add(change); } } DoCellsValueChangeEvent cellChangListEvent = new DoCellsValueChangeEvent { ChangeList = cellChangeList }; didManyThings.Add(cellChangListEvent); currentRow++; } } catch (ArgumentOutOfRangeException ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK); // 粘贴失败,还原到粘贴前 #if !DEBUG if (didManyThings.Count > 0) { DoManyThingsEvent doManyThings = new DoManyThingsEvent { ThingsList = didManyThings }; doManyThings.Undo(dataGridView, null); } didManyThings = null; #endif } catch (Exception ex) { DebugUtility.ShowExceptionMessageBox("粘贴到DataGridView失败", ex); } finally { DidManyThings(didManyThings); m_CSVForm.UpdateGridHeader(); m_CSVForm.AfterChangeCellValue(); } }