private void PasteCell(DataGridView dataGridView, string clipboardStr) { m_CsvForm.BeforeChangeCellValue(); TextBox textBox = dataGridView.EditingControl as TextBox; CellValueChangeItem change = new CellValueChangeItem(); try { change.OldValue = textBox.Text; change.Row = dataGridView.CurrentCell.RowIndex; change.Column = dataGridView.CurrentCell.ColumnIndex; int selectionStart = textBox.SelectionStart; string newValue = textBox.Text; newValue = newValue.Remove(selectionStart, textBox.SelectionLength); newValue = newValue.Insert(selectionStart, clipboardStr); // 在这之后不会抛出异常 textBox.Text = newValue; textBox.SelectionStart = selectionStart + clipboardStr.Length; change.NewValue = newValue; DidCellValueChange(change); } catch (Exception ex) { textBox.Text = change.OldValue; DebugUtility.ShowExceptionMessageBox("粘贴到Cell失败", ex); } finally { m_CsvForm.AfterChangeCellValue(); } }
private void OnReplaceAllButton_Click(object sender, EventArgs e) { if (!MainForm.Instance.SelCsvFormInitialized()) { MessageBox.Show("当前没有打开Csv文件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } DataGridView dataGridView = MainForm.Instance.GetCsvForm().GetDataGridView(); MainForm.Instance.GetCsvForm().BeforeChangeCellValue(); List <CellValueChangeItem> ChangeList = new List <CellValueChangeItem>(); DataGridViewCell cell = Searching(dataGridView, 0, 0); while (cell != null) { CellValueChangeItem changeItem = new CellValueChangeItem(); changeItem.OldValue = (string)cell.Value; changeItem.NewValue = Replacing(changeItem.OldValue); changeItem.Row = cell.RowIndex; changeItem.Column = cell.ColumnIndex; ChangeList.Add(changeItem); cell.Value = changeItem.NewValue; cell = Searching(dataGridView, cell.RowIndex, cell.ColumnIndex); } MainForm.Instance.GetCsvForm().EditManager.DidCellsValueChange(ChangeList); MainForm.Instance.GetCsvForm().AfterChangeCellValue(); }
public void Undo(DataGridView dataGridView, DataTable dataTable) { for (int changeIdx = 0; changeIdx < ChangeList.Count; changeIdx++) { CellValueChangeItem changeItem = ChangeList[changeIdx]; dataGridView.Rows[changeItem.Row].Cells[changeItem.Column].Value = changeItem.OldValue; } }
public void DidCellValueChange(CellValueChangeItem change) { DoCellsValueChangeEvent doCellsValueChangeEvent = new DoCellsValueChangeEvent(); doCellsValueChangeEvent.ChangeList = new List <CellValueChangeItem>(); doCellsValueChangeEvent.ChangeList.Add(change); Did(doCellsValueChangeEvent); }
public void DidCellValueChange(int column, int row, string oldValue, string newValue) { CellValueChangeItem change = new CellValueChangeItem(); change.Column = column; change.Row = row; change.OldValue = oldValue; change.NewValue = newValue; DidCellValueChange(change); }
private void PasetCells(DataGridView dataGridView, string clipboardStr) { // 引用下面两个链接 // https://stackoverflow.com/questions/22833327/pasting-excel-data-into-a-blank-datagridview-index-out-of-range-exception // https://stackoverflow.com/questions/1679778/is-it-possible-to-paste-excel-csv-data-from-clipboard-to-datagridview-in-c List <CellValueChangeItem> changeList = new List <CellValueChangeItem>(); try { m_CsvForm.BeforeChangeCellValue(); string[] lines = Regex.Split(clipboardStr.TrimEnd("\r\n".ToCharArray()), "\r\n"); // 当前行 int currentRow = dataGridView.CurrentCell.RowIndex; // 当前列 int currentCol = dataGridView.CurrentCell.ColumnIndex; DataGridViewCell currentCell; for (int lineIdx = 0; lineIdx < lines.Length; lineIdx++) { 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'); 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; changeList.Add(change); } } currentRow++; } } catch (ArgumentOutOfRangeException ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK); // 粘贴失败,还原到粘贴前 #if !DEBUG if (changeList.Count > 0) { DoCellsValueChangeEvent doCellsValueChangeEvent = new DoCellsValueChangeEvent { ChangeList = changeList }; doCellsValueChangeEvent.Undo(dataGridView, null); } changeList = null; #endif } catch (Exception ex) { DebugUtility.ShowExceptionMessageBox("粘贴到DataGridView失败", ex); } finally { DidCellsValueChange(changeList); m_CsvForm.AfterChangeCellValue(); } }
public void Cut() { if (!CanCut()) { return; } Copy(); if (m_CsvForm.GetDataGridView().IsCurrentCellInEditMode) { m_CsvForm.BeforeChangeCellValue(); TextBox textBox = m_CsvForm.GetDataGridView().EditingControl as TextBox; CellValueChangeItem change = new CellValueChangeItem(); try { int selectionStart = textBox.SelectionStart; change.OldValue = textBox.Text; change.Row = m_CsvForm.GetDataGridView().CurrentCell.RowIndex; change.Column = m_CsvForm.GetDataGridView().CurrentCell.ColumnIndex; change.NewValue = change.OldValue.Remove(textBox.SelectionStart, textBox.SelectionLength); textBox.Text = change.NewValue; textBox.SelectionStart = selectionStart; DidCellValueChange(change); } catch (Exception ex) { textBox.Text = change.OldValue; DebugUtility.ShowExceptionMessageBox("剪切DataGridViewCell数据到剪切板失败", ex); } finally { m_CsvForm.AfterChangeCellValue(); } } else if (MainForm.Instance.GetCellEditTextBox().Focused) { TextBox textBox = MainForm.Instance.GetCellEditTextBox(); int selectionStart = textBox.SelectionStart; textBox.Text = textBox.Text.Remove(textBox.SelectionStart, textBox.SelectionLength); textBox.SelectionStart = selectionStart; } else if (m_CsvForm.GetDataGridView().SelectedCells.Count > 0) { m_CsvForm.BeforeChangeCellValue(); // 转换为Array IList selectedCellList = m_CsvForm.GetDataGridView().SelectedCells; DataGridViewCell[] selectedCells = new DataGridViewCell[selectedCellList.Count]; selectedCellList.CopyTo(selectedCells, 0); CellValueChangeItem[] changes = new CellValueChangeItem[selectedCells.Length]; for (int cellIdx = 0; cellIdx < selectedCells.Length; cellIdx++) { DataGridViewCell cell = selectedCells[cellIdx]; CellValueChangeItem change = new CellValueChangeItem(); change.Row = cell.RowIndex; change.Column = cell.ColumnIndex; change.OldValue = (string)cell.Value; change.NewValue = ""; changes[cellIdx] = change; cell.Value = ""; } DidCellsValueChange(changes.ToList()); m_CsvForm.AfterChangeCellValue(); } }
/// <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(); } }