Пример #1
0
    public void DidManyThings(List <IUndoRedo> thingList)
    {
        if (thingList == null || thingList.Count == 0)
        {
            return;
        }
        DoManyThingsEvent doManyThings = new DoManyThingsEvent
        {
            ThingsList = thingList
        };

        Did(doManyThings);
    }
Пример #2
0
    /// <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();
        }
    }