Exemplo n.º 1
0
        /// <summary>
        /// Pastes tab-delimited data from the clipboard into a range of cells.
        /// </summary>
        private void PasteCells(object sender, GridCellPasteArgs args)
        {
            List <IGridCell>           cellsChanged = new List <IGridCell>();
            List <GridCellChangedArgs> changedArgs  = new List <GridCellChangedArgs>();

            string[] lines = args.Text.Split(Environment.NewLine.ToCharArray()).Where(line => !string.IsNullOrEmpty(line)).ToArray();
            for (int i = 0; i < lines.Length; i++)
            {
                // Add new rows, if necessary.
                while (args.Grid.RowCount < args.StartCell.RowIndex + i + 1)
                {
                    args.Grid.RowCount++;
                }
                string[] words = lines[i].Split('\t');
                int      numReadOnlyColumns = 0;
                for (int j = 0; j < words.Length; j++)
                {
                    // Skip over any read-only columns.
                    IGridColumn column = args.Grid.GetColumn(args.StartCell.ColumnIndex + j + numReadOnlyColumns);
                    while (column != null && column.ReadOnly)
                    {
                        numReadOnlyColumns++;
                        column = args.Grid.GetColumn(args.StartCell.ColumnIndex + j + numReadOnlyColumns);
                    }
                    if (column == null)
                    {
                        throw new Exception("Error pasting into grid - not enough columns.");
                    }

                    int row = args.StartCell.RowIndex + i;
                    int col = args.StartCell.ColumnIndex + j + numReadOnlyColumns;

                    IGridCell cell = args.Grid.GetCell(col, row);

                    string oldValue = null;
                    if (args.Grid.DataSource.Rows.Count > row)
                    {
                        oldValue = args.Grid.DataSource.Rows[row][col]?.ToString();
                    }

                    string newValue = words[j];

                    changedArgs.Add(new GridCellChangedArgs(row, col, oldValue, newValue));

                    // fixme - this should not be set here. What if the item is an array?
                    //cell.Value = Convert.ChangeType(words[j], args.Grid.DataSource.Columns[args.StartCell.ColumnIndex + j + numReadOnlyColumns].DataType);

                    cellsChanged.Add(cell);
                }
            }
            // If some cells were changed then send out an event.
            args.Grid.SelectCells(cellsChanged);
            args.Grid.Refresh(new GridCellsChangedArgs(changedArgs.ToArray()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Pastes tab-delimited data from the clipboard into a range of cells.
        /// </summary>
        private void PasteCells(object sender, GridCellPasteArgs args)
        {
            List <IGridCell> cellsChanged = new List <IGridCell>();
            bool             invalid      = false;

            string[] lines = args.Text.Split(Environment.NewLine.ToCharArray()).Where(line => !string.IsNullOrEmpty(line)).ToArray();
            for (int i = 0; i < lines.Length; i++)
            {
                // Add new rows, if necessary.
                while (args.Grid.RowCount < args.StartCell.RowIndex + i + 1)
                {
                    args.Grid.RowCount++;
                }
                string[] words = lines[i].Split('\t');
                int      numReadOnlyColumns = 0;
                for (int j = 0; j < words.Length; j++)
                {
                    try
                    {
                        // Skip over any read-only columns.
                        IGridColumn column = args.Grid.GetColumn(args.StartCell.ColumnIndex + j + numReadOnlyColumns);
                        while (column != null && column.ReadOnly)
                        {
                            numReadOnlyColumns++;
                            column = args.Grid.GetColumn(args.StartCell.ColumnIndex + j + numReadOnlyColumns);
                        }
                        if (column == null)
                        {
                            throw new Exception("Error pasting into grid - not enough columns.");
                        }
                        IGridCell cell = args.Grid.GetCell(args.StartCell.ColumnIndex + j + numReadOnlyColumns, args.StartCell.RowIndex + i);
                        cell.Value = Convert.ChangeType(words[j], args.Grid.DataSource.Columns[args.StartCell.ColumnIndex + j + numReadOnlyColumns].DataType);
                        cellsChanged.Add(cell);
                    }
                    catch
                    {
                        invalid = true;
                    }
                }
            }
            // If some cells were changed then send out an event.
            args.Grid.SelectCells(cellsChanged);
            args.Grid.Refresh(new GridCellsChangedArgs {
                ChangedCells = cellsChanged, InvalidValue = invalid
            });
        }