Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Insert and Delete Cells

    • 3 minutes to read

    Use the DevExpress Spreadsheet API to insert or delete cells and cell ranges, and control how adjacent cells shift and how inserted cells are formatted.

    Insert Cells

    Call the Worksheet.InsertCells method to insert an empty rectangular range.

    The InsertCells method accepts the following parameters:

    • rowIndex - The zero-based index of the row that contains the top-left cell of the inserted range.
    • columnIndex - The zero-based index of the column that contains the top-left cell of the inserted range.
    • rowCount - The number of rows in the inserted range. The default value is 1.
    • columnCount - The number of columns in the inserted range. The default value is 1.
    • mode - A value that specifies the direction in which existing cells move. The InsertCellsMode enumeration lists the available options: ShiftCellsDown (default), ShiftCellsRight.
    • format - A value that specifies the formatting for the inserted cells. The InsertFormatMode enumeration lists the available options: SameAsPrevious (default), SameAsNext, None.

    The following code snippet inserts two empty cells in column B and shifts existing cells down:

    using DevExpress.Docs.Spreadsheet;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.Cells[1, 1].Value = "Existing value";
    worksheet.Cells[2, 1].Value = "Another value";
    
    worksheet.InsertCells(1, 1, 2, 1, InsertCellsMode.ShiftCellsDown, InsertFormatMode.SameAsPrevious);
    
    // Insert a single cell at row 2, column B with default options
    worksheet.InsertCells(1, 1);
    

    Delete Cells

    Call the Worksheet.DeleteCells method to remove a rectangular range.

    The DeleteCells method accepts the following parameters:

    • rowIndex - The zero-based index of the row that contains the top-left cell of the deleted range.
    • columnIndex - The zero-based index of the column that contains the top-left cell of the deleted range.
    • rowCount - The number of rows in the deleted range.
    • columnCount - The number of columns in the deleted range.
    • mode - A value that specifies the direction in which remaining cells move. The DeleteCellsMode enumeration lists the available options: ShiftCellsUp (default), ShiftCellsLeft.

    The following code snippet deletes two cells in column B and shifts cells below the range up:

    using DevExpress.Docs.Spreadsheet;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.Cells[1, 1].Value = "Remove";
    worksheet.Cells[2, 1].Value = "Also remove";
    worksheet.Cells[3, 1].Value = "Moved up";
    
    worksheet.DeleteCells(1, 1, 2, 1, DeleteCellsMode.ShiftCellsUp);
    

    Insert and Delete Rows and Columns

    To insert or delete entire rows or columns, use the methods described in Access and Manage Rows and Columns.