How to: Delete a Row or Column from a Worksheet
- 3 minutes to read
Row
This example demonstrates how to remove rows from a worksheet.
- Call the Row.Delete method to delete the current row.
- Call the RowCollection.Remove method of the Worksheet.Rows collection to remove a row at the specified position or delete multiple rows at once.
- To delete a row containing the specified cell or multiple rows containing the specified cell range, use the Worksheet.DeleteCells method with DeleteMode.EntireRow enumeration member passed as a parameter.
When you delete rows from a worksheet, other rows are automatically shifted up.
Enclose your code in the SpreadsheetControl.BeginUpdate - SpreadsheetControl.EndUpdate method calls to suppress the SpreadsheetControl’s visual updates and improve its performance when you remove multiple rows from a document.
// Delete the second row from the worksheet.
worksheet.Rows[1].Delete();
// Delete the third row from the worksheet.
worksheet.Rows.Remove(2);
// Delete three rows from the worksheet starting from the tenth row.
worksheet.Rows.Remove(9, 3);
// Delete a row that contains the "B2" cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireRow);
You can also hide worksheet rows. See the How to: Show or Hide a Row or Column topic for details.
Note
The number of rows in a worksheet is permanently fixed - 1,048,576.
After rows have been removed from a worksheet via the control’s UI, the SpreadsheetControl.RowsRemoved event is raised.
Column
This example demonstrates how to remove columns from a worksheet.
- Call the Column.Delete method to delete the current column.
- Call the ColumnCollection.Remove method of the Worksheet.Columns collection to remove a column at the specified position or delete multiple columns at once.
- To delete a column containing the specified cell or multiple columns containing the specified cell range, use the Worksheet.DeleteCells method with the DeleteMode.EntireColumn enumeration member passed as a parameter.
When you delete columns from a worksheet, other columns are automatically shifted to the left.
Enclose your code in the SpreadsheetControl.BeginUpdate - SpreadsheetControl.EndUpdate method calls to suppress the SpreadsheetControl’s visual updates and improve its performance when you remove multiple columns from a document.
// Delete the second column from the worksheet.
worksheet.Columns[1].Delete();
// Delete the third column from the worksheet.
worksheet.Columns.Remove(2);
// Delete three columns from the worksheet starting from the tenth column.
worksheet.Columns.Remove(9, 3);
// Delete a column that contains the "B2" cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireColumn);
You can also hide worksheet columns. See the How to: Show or Hide a Row or Column topic for details.
Note
The number of columns in a worksheet is permanently fixed - 16,384.
After columns have been removed from a worksheet via the control’s UI, the SpreadsheetControl.ColumnsRemoved event is raised.