Back to Devexpress

Worksheet.DeleteCells(CellRange, DeleteMode) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-dot-deletecells-x28-devexpress-dot-spreadsheet-dot-cellrange-devexpress-dot-spreadsheet-dot-deletemode-x29.md

latest11.4 KB
Original Source

Worksheet.DeleteCells(CellRange, DeleteMode) Method

Deletes cells from the worksheet.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void DeleteCells(
    CellRange range,
    DeleteMode mode
)
vb
Sub DeleteCells(
    range As CellRange,
    mode As DeleteMode
)

Parameters

NameTypeDescription
rangeCellRange

A CellRange object specifying a cell range to be deleted from the worksheet.

| | mode | DeleteMode |

A DeleteMode enumeration member specifying in which direction to shift other cells.

|

Remarks

Use the DeleteCells method to delete the specified cell range from the worksheet.

You can delete a cell range and shift other cells in the same column up, or shift other cells in the same row to the left. You can also delete entire rows and columns that comprise the specified range of cells. In this case, other rows and columns are automatically shifted up or to the left. For more information on deleting rows and columns, refer to the How to: Delete a Row or Column from a Worksheet example.

To delete only cell content or formatting without removing entire cells from the worksheet, use the Worksheet.ClearContents, Worksheet.ClearFormats, Worksheet.ClearHyperlinks, Worksheet.ClearComments or Worksheet.Clear method (see the How to: Clear Cells of Content, Formatting, Hyperlinks and Comments example).

To insert new cells into the worksheet, call the Worksheet.InsertCells method (see the How to: Insert a Cell or Cell Range example).

Another way to insert, delete and clear the cell range is to use the range’s RangeExtensions.Insert, RangeExtensions.Delete and Clear* 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 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)

The following code snippets (auto-collected from DevExpress Examples) contain references to the DeleteCells(CellRange, DeleteMode) 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#L81

csharp
// Delete a row that contains the "B2" cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireRow);
#endregion #DeleteRows

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

csharp
// Delete a row that contains the "B2"cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireRow);
#endregion #DeleteRows

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

csharp
// Delete a row that contains the "B2" cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireRow);
#endregion #DeleteRows

spreadsheet-document-api-import-data-from-data-sources/CS/DataImportExample/Form1.cs#L17

csharp
Worksheet worksheet = spreadsheetControl1.Document.Worksheets[0];
worksheet.DeleteCells(worksheet.Cells, DeleteMode.ShiftCellsLeft);
ImportDataTable();

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

vb
' Delete a row that contains the "B2" cell.
                worksheet.DeleteCells(worksheet.Cells("B2"), DeleteMode.EntireRow)
' #End Region ' #DeleteRows

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

vb
' Delete a row that contains the "B2"cell.
                worksheet.DeleteCells(worksheet.Cells("B2"), DeleteMode.EntireRow)
' #End Region ' #DeleteRows

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

vb
' Delete a row that contains the "B2" cell.
            worksheet.DeleteCells(worksheet.Cells("B2"), DeleteMode.EntireRow)
#End Region ' #DeleteRows

spreadsheet-document-api-import-data-from-data-sources/VB/DataImportExample/Form1.vb#L19

vb
Dim worksheet As Worksheet = spreadsheetControl1.Document.Worksheets(0)
worksheet.DeleteCells(worksheet.Cells, DeleteMode.ShiftCellsLeft)
ImportDataTable()

See Also

Row.Delete

RowCollection.Remove*

Column.Delete

ColumnCollection.Remove*

Clear(CellRange)

ClearContents(CellRange)

ClearFormats(CellRange)

ClearHyperlinks(CellRange)

ClearComments(CellRange)

How to: Delete a Row or Column from a Worksheet

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

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace