Back to Devexpress

How to: Delete a Cell or Range of Cells

officefileapi-14764-spreadsheet-document-api-examples-cells-how-to-delete-a-cell-or-range-of-cells.md

latest2.6 KB
Original Source

How to: Delete a Cell or Range of Cells

  • Sep 19, 2023
  • 2 minutes to read

This example demonstrates how to delete cells from a worksheet. To do this, use the Worksheet.DeleteCells method. Pass a cell or cell range that you want to delete, and the DeleteMode.ShiftCellsUp or DeleteMode.ShiftCellsLeft enumeration member to specify how to shift other cells.

csharp
using DevExpress.Spreadsheet;
// ...

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

// Delete the C5 cell, shifting other cells in the same row to the left.
worksheet.DeleteCells(worksheet.Cells["C5"], DeleteMode.ShiftCellsLeft);

// Delete the H11:I12 range of cells, shifting other cells in the same column up.
worksheet.DeleteCells(worksheet.Range["H11:I12"], DeleteMode.ShiftCellsUp);
vb
Imports DevExpress.Spreadsheet
' ...

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

' Delete the C5 cell, shifting other cells in the same row to the left.
worksheet.DeleteCells(worksheet.Cells("C5"), DeleteMode.ShiftCellsLeft)

' Delete the H11:I12 range of cells, shifting other cells in the same column up.
worksheet.DeleteCells(worksheet.Range("H11:I12"), DeleteMode.ShiftCellsUp)

To delete cell content or formatting without removing an entire cell from the worksheet, use the Clear* methods of the Worksheet object (see the How to: Clear Cells of Content, Formatting, Hyperlinks and Comments example).

See Also

How to: Insert a Cell or Cell Range

How to: Delete a Row or Column from a Worksheet

How to: Clear Cells of Content, Formatting, Hyperlinks and Comments