/// <summary> /// Deletes data from a cell or range of cells. /// If the first cell is the same as the last cell, only the data from that cell will be copied. /// </summary> private void DeleteCells(object sender, GridCellActionArgs args) { if (args.Grid.ReadOnly) { throw new Exception("Unable to delete cells - grid is read-only."); } List <IGridCell> cellsChanged = new List <IGridCell>(); for (int row = args.StartCell.RowIndex; row <= args.EndCell.RowIndex; row++) { for (int column = args.StartCell.ColumnIndex; column <= args.EndCell.ColumnIndex; column++) { if (!args.Grid.GetColumn(column).ReadOnly) { args.Grid.DataSource.Rows[row][column] = DBNull.Value; cellsChanged.Add(grid.GetCell(column, row)); } } if (args.Grid.CanGrow && args.StartCell.ColumnIndex == 0 && args.EndCell.ColumnIndex == args.Grid.DataSource.Columns.Count - 1) { // User has selected the entire row. In this case, we delete the entire row, // but only if the grid can change size. args.Grid.DataSource.Rows.Remove(args.Grid.DataSource.Rows[row]); } } // If some cells were changed then we will need to update the model. if (cellsChanged.Count > 0) { args.Grid.Refresh(new GridCellsChangedArgs() { ChangedCells = cellsChanged }); } }
/// <summary> /// Copies data from a cell or range of cells to the clipboard. /// If the first cell is the same as the last cell, only the data from that cell will be copied. /// </summary> private void CopyCells(object sender, GridCellActionArgs args) { StringBuilder textToCopy = new StringBuilder(); for (int row = args.StartCell.RowIndex; row <= args.EndCell.RowIndex; row++) { for (int column = args.StartCell.ColumnIndex; column <= args.EndCell.ColumnIndex; column++) { textToCopy.Append(grid.GetCell(column, row).Value.ToString()); if (column != args.EndCell.ColumnIndex) { textToCopy.Append('\t'); } } textToCopy.AppendLine(); } presenter.SetClipboardText(textToCopy.ToString(), "CLIPBOARD"); }