CellRange.CopyFrom(CellRange) Method
Copies the specified range of cells into the current range.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Spreadsheet.v24.1.Core.dll
NuGet Package: DevExpress.Spreadsheet.Core
Declaration
Parameters
Name | Type | Description |
---|---|---|
source | CellRange | A CellRange object that specifies the cell range to be copied. |
Remarks
Relative cell references contained in the source range are automatically adjusted when being copied into the current range. If the source range includes hidden cells (cells that belong to hidden rows or columns), these cells are also copied.
If the current cell range and the specified source cell range are not the same size, data is pasted as follows.
If all of the source ranges fit into the current range, the copied data is pasted consecutively to completely fill the current range.
If all of the source ranges cannot fit into the current range, the copied data is pasted once starting with the top left cell of the current range.
If the current range contains a single row and all of the source ranges fit into this range width, the copied data is pasted consecutively within the current range width.
If the current range contains a single row but all of the source ranges cannot fit into this range width, the copied data is pasted once starting with the first cell of the current range.
If the current range contains a single column and all of the source ranges fit into this range height, the copied data is pasted consecutively within the current range height.
If the current range contains a single column but all of the source ranges cannot fit into this range height, the copied data is pasted once starting with the first cell of the current range.
Note
The CopyFrom
method doesn’t copy tags from the source range to the target range.
Example
This example demonstrates how to copy data from one cell to another, and specify which parts of the copied data (for example, value only, formatting information only, borders only, etc.) should be pasted into the destination cell. To do this, call the CellRange.CopyFrom method of the Cell object corresponding to the cell into which you want to copy data from another cell. Pass the source cell object and required member of the PasteSpecial enumerator as parameters.
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Columns["A"].WidthInCharacters = 32;
worksheet.Columns["B"].WidthInCharacters = 20;
Style style = workbook.Styles[BuiltInStyleId.Input];
// Specify the content and formatting for a source cell.
worksheet.Cells["A1"].Value = "Source Cell";
Cell sourceCell = worksheet.Cells["B1"];
sourceCell.Formula = "= PI()";
sourceCell.NumberFormat = "0.0000";
sourceCell.Style = style;
sourceCell.Font.Color = Color.Blue;
sourceCell.Font.Bold = true;
sourceCell.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin);
// Copy all information from the source cell to the "B3" cell.
worksheet.Cells["A3"].Value = "Copy All";
worksheet.Cells["B3"].CopyFrom(sourceCell);
// Copy only the source cell content (e.g., text, numbers, formula calculated values) to the "B4" cell.
worksheet.Cells["A4"].Value = "Copy Values";
worksheet.Cells["B4"].CopyFrom(sourceCell, PasteSpecial.Values);
// Copy the source cell content (e.g., text, numbers, formula calculated values)
// and number formats to the "B5" cell.
worksheet.Cells["A5"].Value = "Copy Values and Number Formats";
worksheet.Cells["B5"].CopyFrom(sourceCell, PasteSpecial.Values | PasteSpecial.NumberFormats);
// Copy only the formatting information from the source cell to the "B6" cell.
worksheet.Cells["A6"].Value = "Copy Formats";
worksheet.Cells["B6"].CopyFrom(sourceCell, PasteSpecial.Formats);
// Copy all information from the source cell to the "B7" cell except for border settings.
worksheet.Cells["A7"].Value = "Copy All Except Borders";
worksheet.Cells["B7"].CopyFrom(sourceCell, PasteSpecial.All & ~PasteSpecial.Borders);
// Copy information only about borders from the source cell to the "B8" cell.
worksheet.Cells["A8"].Value = "Copy Borders";
worksheet.Cells["B8"].CopyFrom(sourceCell, PasteSpecial.Borders);
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the CopyFrom(CellRange) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.