Back to Devexpress

Worksheet.Rows Property

officefileapi-devexpress-dot-spreadsheet-dot-worksheet-0f1a7a34.md

latest15.7 KB
Original Source

Worksheet.Rows Property

Returns the collection of all rows in a worksheet.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
RowCollection Rows { get; }
vb
ReadOnly Property Rows As RowCollection

Property Value

TypeDescription
RowCollection

The collection of Row objects.

|

Remarks

Use the RowCollection object’s members to access, add, hide, group, and remove rows.

Access Rows

Use the RowCollection.Item property to access an individual row in a worksheet.

Obtain the Row at the Specified Index

csharp
using DevExpress.Spreadsheet;
// ...

// Access the row collection.
RowCollection rows = workbook.Worksheets[0].Rows;

// Access the first row in the collection.
Row firstRow = rows[0];
vb
Imports DevExpress.Spreadsheet
' ...

' Access the row collection.
Dim rows As RowCollection = workbook.Worksheets(0).Rows

' Access the first row in the collection.
Dim firstRow As Row = rows(0)

A row index is zero-based. It specifies the row position in the collection.

Obtain the Row with a Given Heading

csharp
using DevExpress.Spreadsheet;
// ...

// Access the row collection.
RowCollection rows = workbook.Worksheets[0].Rows;

// Access the first row by its heading.
Row firstRow = rows["1"];
vb
Imports DevExpress.Spreadsheet
' ...

' Access the row collection.
Dim rows As RowCollection = workbook.Worksheets(0).Rows

' Access the first row by its heading.
Dim firstRow As Row = rows("1")

Row headings help users identify each row in a worksheet.

Insert Rows

Use the following methods to insert rows into a worksheet:

Row.InsertInserts a row above the current row.RowCollection.InsertInserts rows at the specified position.

Note

The number of rows in a worksheet does not change - 1,048,576. When you add new rows, the rows below are shifted down and an equivalent number of rows at the end of the worksheet is removed.

Insert a Single Row

csharp
// Insert the third row.
worksheet.Rows["3"].Insert();

// Insert the fifth row.
worksheet.Rows.Insert(4);
vb
' Insert the third row.
worksheet.Rows("3").Insert()

' Insert the fifth row.
worksheet.Rows.Insert(4)

Insert Multiple Rows

csharp
// Insert three rows (from row 3 to row 5).
worksheet.Rows.Insert(2, 3);
vb
' Insert three rows (from row 3 to row 5).
worksheet.Rows.Insert(2, 3)

Insert Rows Above a Specific Cell Range

Use the Worksheet.InsertCells method to insert empty rows above the specified cells. The method inserts the same number of rows as the specified cell range.

csharp
// Insert two rows above the "B3:E4" cell range.
worksheet.InsertCells(worksheet.Range["B3:E4"], InsertCellsMode.EntireRow);
vb
' Insert two rows above the "B3:E4" cell range.
worksheet.InsertCells(worksheet.Range("B3:E4"), InsertCellsMode.EntireRow)

Format Inserted Rows

Pass a RowFormatMode enumeration member to the RowCollection.Insert method to specify format options for inserted rows.

csharp
// Insert two rows with the same formatting as the first row.
worksheet.Rows.Insert(1, 2, RowFormatMode.FormatAsPrevious);
vb
' Insert two rows with the same formatting as the first row.
worksheet.Rows.Insert(1, 2, RowFormatMode.FormatAsPrevious)

Remove Rows

Use the following methods to delete rows from a worksheet:

Row.DeleteRemoves the current row.RowCollection.RemoveRemoves rows at the specified position.

Note

The number of rows in a worksheet does not change - 1,048,576. When you delete rows, the rows below are shifted up and an equivalent number of new rows is added to the end of the worksheet.

Remove a Single Row

csharp
// Delete the second row.
worksheet.Rows[1].Delete();

// Delete the third row.
worksheet.Rows.Remove(2);
vb
' Delete the second row.
worksheet.Rows(1).Delete()

' Delete the third row.
worksheet.Rows.Remove(2)

Remove Multiple Rows

csharp
// Delete three rows (from row 2 to row 4).
worksheet.Rows.Remove(1, 3);
vb
' Delete three rows (from row 2 to row 4).
worksheet.Rows.Remove(1, 3)

Remove Rows That Meet a Specific Condition

csharp
// Specify the condition to remove worksheet rows.
// If a value in the "D" column is greater than 30, remove the corresponding row. 
Func<int, bool> rowRemovalCondition = x => worksheet.Cells[x, 3].Value.NumericValue > 30.0;

// Delete rows that meet the specified condition.
// Check rows 2 through 7.
worksheet.Rows.Remove(1, 6, rowRemovalCondition);
vb
' Specify the condition to remove worksheet rows.
' If a value in the "D" column is greater than 30, remove the corresponding row. 
Dim rowRemovalCondition As Func(Of Integer, Boolean) = Function(x) worksheet.Cells(x, 3).Value.NumericValue > 30.0

' Delete rows that meet the specified condition.
' Check rows 2 through 7.
worksheet.Rows.Remove(1, 6, rowRemovalCondition)

Remove Rows That Contain Specific Cells

Use the Worksheet.DeleteCells method to delete rows that contain the specified cell range.

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

Group and Ungroup Rows

Use the RowCollection.Group method to group rows.

csharp
// Group rows 3 through 6 and collapse the group.
worksheet.Rows.Group(2, 5, true);

// Group rows 9 through 12 and expand the group.
worksheet.Rows.Group(8, 11, false);

// Group rows 2 through 13 to create the outer group. 
worksheet.Rows.Group(1, 12, false);
vb
' Group rows 3 through 6 and collapse the group.
worksheet.Rows.Group(2, 5, True)

' Group rows 9 through 12 and expand the group.
worksheet.Rows.Group(8, 11, False)

' Group rows 2 through 13 to create the outer group. 
worksheet.Rows.Group(1, 12, False)

Use the RowCollection.UnGroup method to ungroup rows.

csharp
// Ungroup rows 3 through 6 and display collapsed data.
worksheet.Rows.UnGroup(2, 5, true);

// Ungroup rows 9 through 12.
worksheet.Rows.UnGroup(8, 11, false);

// Remove the outer row group.
worksheet.Rows.UnGroup(1, 12, false);
vb
' Ungroup rows 3 through 6 and display collapsed data.
worksheet.Rows.UnGroup(2, 5, True)

' Ungroup rows 9 through 12.
worksheet.Rows.UnGroup(8, 11, False)

' Remove the outer row group.
worksheet.Rows.UnGroup(1, 12, False)

Expand or Collapse Groups

Use the following methods to collapse or expand row groups in a worksheet:

RowCollection.CollapseAllGroupsCollapses all row groups.RowCollection.CollapseGroupsCollapses row groups starting with the specified group level.RowCollection.ExpandAllGroupsExpands all row groups.

csharp
// Collapse all row groups except the first level group.
worksheet.Rows.CollapseGroups(2);
vb
' Collapse all row groups except the first level group.
worksheet.Rows.CollapseGroups(2)

Hide Rows

Use the Row.Visible property or the RowCollection.Hide method to hide rows in a worksheet.

csharp
// Hide rows 2 through 6.
worksheet.Rows.Hide(1, 5);

// Hide row 8.
worksheet.Rows[7].Visible = false;
vb
' Hide rows 2 through 6.
worksheet.Rows.Hide(1, 5)

' Hide row 8.
worksheet.Rows(7).Visible = False

Set the Row.Visible property to true to display a row. Use the RowCollection.Unhide method to show multiple hidden rows.

csharp
// Show rows 2 through 6.
worksheet.Rows.Unhide(1, 5);

// Show row 8.
worksheet.Rows[7].Visible = true;
vb
' Show rows 2 through 6.
worksheet.Rows.Unhide(1, 5)

' Show row 8.
worksheet.Rows(7).Visible = True

The following code snippets (auto-collected from DevExpress Examples) contain references to the Rows property.

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#L121

csharp
// Copy all data from the 2nd row to the 5th row.
worksheet.Rows["5"].CopyFrom(worksheet.Rows["2"]);

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

csharp
// Copy all data from the 2nd row to the 5th row.
worksheet.Rows["5"].CopyFrom(worksheet.Rows["2"]);

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

csharp
// Copy all data from the second row to the fifth row.
worksheet.Rows["5"].CopyFrom(worksheet.Rows["2"]);

asp-net-core-spreadsheet-update-document/CS/UpdateDocumentUsingAJAX/Controllers/HomeController.cs#L44

csharp
"Security", "Training classes", "Web site hosting", "Web site updates"};
Row fistRow = worksheet.Rows[0];
fistRow.ColumnWidthInCharacters = 6;

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

vb
' Copy all data from the 2nd row to the 5th row.
worksheet.Rows("5").CopyFrom(worksheet.Rows("2"))

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

vb
' Copy all data from the 2nd row to the 5th row.
worksheet.Rows("5").CopyFrom(worksheet.Rows("2"))

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

vb
' Copy all data from the second row to the fifth row.
worksheet.Rows("5").CopyFrom(worksheet.Rows("2"))
' Copy only borders from the "B" column to the "E" column.

See Also

Rows and Columns in Spreadsheet Documents

Worksheet Interface

Worksheet Members

DevExpress.Spreadsheet Namespace