Insert and Remove Rows and Columns
Worksheets in RadSpreadProcessing's document model consist of cells organized in rows and columns. Each worksheet allows you to insert and remove rows and columns through shifting the contents of the surrounding rows and columns. This article demonstrates how to insert and remove rows and columns.
Insert Rows
In order to insert rows, you need to create a RowSelection instance that indicates where the new rows are to be inserted in the worksheet. Whenever rows insertion is performed, all values that appear down of the RowSelection region including the selected region are shifted down, thus, causing no loss of data.
The RowSelection class exposes CanInsert() and Insert() methods that indicate whether the insert is possible and perform the insert operation respectively. Example 1 shows how to insert rows using the two methods.
Example 1: Insert rows
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add();
int index = 2;
int itemCount = 3;
if (worksheet.Rows.CanInsert(index, itemCount))
{
RowSelection selection = worksheet.Rows[index, index + itemCount];
selection.Insert();
}
Remove Rows
In order to remove rows, you need to create a RowSelection instance that specifies the region of rows you would like to remove. Whenever you remove rows, all values that appear down of the RowSelection region are shifted up.
The RowSelection class exposes a Remove() method that performs the removal of the selected rows. Example 2 shows how to remove rows.
Example 2: Remove rows
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add();
int startIndex = 2;
int endIndex = 4;
RowSelection selection = worksheet.Rows[startIndex, endIndex];
selection.Remove();
Insert Columns
In order to insert columns, you need to create a ColumnSelection instance that specifies where the new columns are to be inserted in the worksheet. Whenever columns insertion is performed, all values that appear to the right of the ColumnSelection region including the selected region are shifted right, thus, causing no loss of data.
The ColumnSelection class exposes CanInsert() and Insert() methods that indicate whether the insert is possible and perform the insert operation respectively. Example 3 shows how to insert columns using the two methods.
Example 3: Insert columns
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add();
int index = 2;
int itemCount = 3;
if (worksheet.Columns.CanInsert(index, itemCount))
{
ColumnSelection selection = worksheet.Columns[index, index + itemCount];
selection.Insert();
}
Remove Columns
In order to remove columns, you need to create a ColumnSelection instance that indicates the region of columns you would like to remove. Whenever you remove columns, all values that appear to the right of the ColumnSelection region are shifted to the left.
The ColumnSelection class exposes a Remove() method that executes the removal of the selected columns. Example 4 illustrates how to remove columns.
Example 4: Remove columns
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets.Add();
int startIndex = 2;
int endIndex = 4;
ColumnSelection selection = worksheet.Columns[startIndex, endIndex];
selection.Remove();