Back to Devexpress

Worksheet.InsertCells(CellRange, InsertCellsMode) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-insertcells-x28-devexpress-dot-spreadsheet-dot-cellrange-devexpress-dot-spreadsheet-dot-insertcellsmode-x29.md

latest8.8 KB
Original Source

Worksheet.InsertCells(CellRange, InsertCellsMode) Method

Inserts new cells in the worksheet.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void InsertCells(
    CellRange range,
    InsertCellsMode mode
)
vb
Sub InsertCells(
    range As CellRange,
    mode As InsertCellsMode
)

Parameters

NameTypeDescription
rangeCellRange

A CellRange object specifying a cell range where new cells should be inserted.

| | mode | InsertCellsMode |

An InsertCellsMode enumeration member specifying in which direction to shift other cells.

|

Remarks

Use the InsertCells method to insert new cells at the position of the specified cell range.

You can insert a new range and shift other cells in the same column down, or shift other cells in the same row to the right. You can also insert empty rows and columns above or to the left of the specified range of cells, respectively (the same number of rows or columns that comprise the specified range).

To delete cells, use the Worksheet.DeleteCells method (see the How to: Delete a Cell or Range of Cells example).

Another way to insert and delete the cell range is to use the range’s RangeExtensions.Insert and RangeExtensions.Delete extension methods, defined by the RangeExtensions class. These extension methods are accessible as methods of the CellRange object and called by using the instance method syntax.

Example

This example demonstrates how to insert cells into a worksheet. To do this, use the Worksheet.InsertCells method. Pass a cell or cell range where you want to insert new cells, and the InsertCellsMode.ShiftCellsDown or InsertCellsMode.ShiftCellsRight enumeration member to specify how to shift other cells.

csharp
using DevExpress.Spreadsheet;
// ...

IWorkbook workbook = spreadsheetControl1.Document;
Worksheet worksheet = workbook.Worksheets[0];

// Insert a cell into the C5 position, shifting other cells in the same column down.
worksheet.InsertCells(worksheet.Cells["C5"], InsertCellsMode.ShiftCellsDown);

// Insert cells into the location of the H11:I12 range, shifting other cells in the same row to the right.
worksheet.InsertCells(worksheet.Range["H11:I12"], InsertCellsMode.ShiftCellsRight);
vb
Imports DevExpress.Spreadsheet
' ...

Dim workbook As IWorkbook = spreadsheetControl1.Document
Dim worksheet As Worksheet = workbook.Worksheets(0)

' Insert a cell into the C5 position, shifting other cells in the same column down.
worksheet.InsertCells(worksheet.Cells("C5"), InsertCellsMode.ShiftCellsDown)

' Insert cells into the location of the H11:I12 range, shifting other cells in the same row to the right.
worksheet.InsertCells(worksheet.Range("H11:I12"), InsertCellsMode.ShiftCellsRight)

The following code snippets (auto-collected from DevExpress Examples) contain references to the InsertCells(CellRange, InsertCellsMode) 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.

winforms-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/RowAndColumnActions.cs#L37

csharp
// Insert two rows above the "L15:M16" cell range.
worksheet.InsertCells(worksheet.Range["L15:M16"], InsertCellsMode.EntireRow);
#endregion #InsertRows

wpf-spreadsheetcontrol-api-part-1/CS/SpreadsheetControl_WPF_API/SpreadsheetActions/RowAndColumnActions.cs#L38

csharp
// Insert two rows above the "L15:M16" cell range.
worksheet.InsertCells(worksheet.Range["L15:M16"], InsertCellsMode.EntireRow);
#endregion #InsertRows

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/RowAndColumnActions.cs#L39

csharp
// Insert two rows above the "L15:M16" cell range.
worksheet.InsertCells(worksheet.Range["L15:M16"], InsertCellsMode.EntireRow);
#endregion #InsertRows

winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/RowAndColumnActions.vb#L41

vb
' Insert two rows above the "L15:M16" cell range.
                worksheet.InsertCells(worksheet.Range("L15:M16"), InsertCellsMode.EntireRow)
' #End Region ' #InsertRows

wpf-spreadsheetcontrol-api-part-1/VB/SpreadsheetControl_WPF_API/SpreadsheetActions/RowAndColumnActions.vb#L41

vb
' Insert two rows above the "L15:M16" cell range.
                worksheet.InsertCells(worksheet.Range("L15:M16"), InsertCellsMode.EntireRow)
' #End Region ' #InsertRows

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/RowAndColumnActions.vb#L34

vb
' Insert two rows above the "L15:M16" cell range.
            worksheet.InsertCells(worksheet.Range("L15:M16"), InsertCellsMode.EntireRow)
#End Region ' #InsertRows

See Also

Row.Insert

RowCollection.Insert*

Column.Insert

ColumnCollection.Insert*

How to: Add a New Row or Column to a Worksheet

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace