Exemplo n.º 1
0
    public void DidCellValueChange(CellValueChangeItem change)
    {
        DoCellsValueChangeEvent doCellsValueChangeEvent = new DoCellsValueChangeEvent();

        doCellsValueChangeEvent.ChangeList = new List <CellValueChangeItem>();
        doCellsValueChangeEvent.ChangeList.Add(change);

        Did(doCellsValueChangeEvent);
    }
Exemplo n.º 2
0
    public void DidCellsValueChange(List <CellValueChangeItem> changeList)
    {
        if (changeList == null || changeList.Count == 0)
        {
            return;
        }
        DoCellsValueChangeEvent doCellsValueChangeEvent = new DoCellsValueChangeEvent
        {
            ChangeList = changeList
        };

        Did(doCellsValueChangeEvent);
    }
Exemplo n.º 3
0
    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();
        }
    }
Exemplo n.º 4
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();
        }
    }