You can swap the contents of two cells or two ranges of cells.
When you swap data from a cell or a range of cells to another cell or range of cells, the settings for the cell are swapped along with the data. If you provided settings for the column or the row containing the cell, or the spreadsheet, but not the cell itself, those settings are not swapped. For example, if you have set the source cell background color to red, the background color is swapped and the target cell has a red background. However, if you have set the background color of the column containing the source cell to red, that setting is not swapped.
When you swap data from one cell to another, the data in one cell becomes the data in the other cell, and vice versa. For example, if cell A1 contains the value 4 and cell B3 contains the value 6 and you swap the values of the cells, the value of cell A1 becomes 6 and the value of cell B3 becomes 4.
If you attempt to swap a range that is larger than the available range at the destination, the swap operation is not performed. For example, if you attempt to swap a range of four cells and specify the destination as a cell at the edge of the spreadsheet, the swap does not take place.
If the swap operation swaps overlapping ranges of cells, individual cells are swapped starting at the overlapping corner.
If the ranges overlap, such as moving rows 1 and 2 before row 0, you can add extra rows, move the rows, and then remove the extra rows.
For more information on methods to move or swap data, refer to their page in the API reference:
- SheetView Class: SwapRange Method
- SheetView Class: MoveRange Method
- Model Namespace: DefaultSheetSpanModel Class: SwapColumns Method
- Model Namespace: DefaultSheetSpanModel Class: SwapRows Method
Using Code
Here is an example of swapping a range of cells.
Example
The first example swaps a range and the second example adds rows, moves rows, and then removes the extra rows.
C# | Copy Code |
---|---|
fpSpread1.ActiveSheet.RowCount = 10; fpSpread1.ActiveSheet.ColumnCount = 10; private void button1_Click(object sender, System.EventArgs e) { fpSpread1.ActiveSheet.SwapRange(0, 0, 3, 0, 3, 3, true); } |
or
C# | Copy Code |
---|---|
fpSpread1.ActiveSheet.RowCount = 10; fpSpread1.ActiveSheet.ColumnCount = 10; private void button1_Click(object sender, System.EventArgs e) { fpSpread1.ActiveSheet.Rows[0,1].Add() ; fpSpread1.ActiveSheet.MoveRange(3,0,0,0,2,4,true); fpSpread1.ActiveSheet.Rows[3,4].Remove(); } |
VB | Copy Code |
---|---|
FpSpread1.ActiveSheet.RowCount = 10 FpSpread1.ActiveSheet.ColumnCount = 10 Private Sub void button1_Click(sender As Object, e As System.EventArgs) FpSpread1.ActiveSheet.SwapRange(0, 0, 3, 0, 3, 3, True) End Sub 'button1_Click |
VB | Copy Code |
---|---|
FpSpread1.ActiveSheet.RowCount = 10 FpSpread1.ActiveSheet.ColumnCount = 10 Private Sub void button1_Click(sender As Object, e As System.EventArgs) FpSpread1.ActiveSheet.Rows(0,1).Add() FpSpread1.ActiveSheet.MoveRange(3,0,0,0,2,4,True) FpSpread1.ActiveSheet.Rows(3,4).Remove() End Sub 'button1_Click |
When you swap ranges of data, you can specify whether formulas are adjusted. For more information, see Recalculating and Updating Formulas Automatically.