Insert and Remove Rows and Columns
Worksheets in the RadSpreadProcessing 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. The following sections show how to insert and remove rows and columns.
Insert Rows
To insert rows, create a RowSelection instance that indicates where the new rows are to be inserted in the worksheet. When row insertion is performed, all values that appear below the RowSelection region including the selected region are shifted down, 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
To remove rows, create a RowSelection instance that specifies the region of rows you want to remove. When you remove rows, all values that appear below 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
To insert columns, create a ColumnSelection instance that specifies where the new columns are to be inserted in the worksheet. When column insertion is performed, all values that appear to the right of the ColumnSelection region including the selected region are shifted right, 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
To remove columns, create a ColumnSelection instance that indicates the region of columns you want to remove. When 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 shows 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();