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
  • Access and Manage Rows and Columns

    • 5 minutes to read

    A worksheet supports up to 1,048,576 rows and 16,384 columns. Row and column indexes start at zero.

    Use Worksheet.Rows and Worksheet.Columns properties to access rows and columns. These properties return RowCollection and ColumnCollection objects, respectively.

    Access Rows and Columns

    Use a zero-based row or column index to access a specific row or column. Rows and Columns collections can be empty even though you can access any valid row or column index. The API creates a Row or Column object for the specified index when accessed.

    The following code snippet accesses the row at index 5 and the column at index 3:

    Worksheet worksheet = workbook.Worksheets[0];
    
    // Access the row at index 5.
    Row row5 = worksheet.Rows[5];
    
    // Access the column at index 3.
    Column column3 = worksheet.Columns[3];
    

    Note

    RowCollection.Count/ColumnCollection.Count property returns the number of rows/columns that have customized settings (such as width, height, visibility, and style).

    You can also access a row or column by its heading. Pass a row heading (for example, “6”) to the RowCollection.Item(rowHeading) indexer or a column heading (for example, “D”) to the ColumnCollection.Item(columnHeading) indexer. The Column.Heading property gets the column heading.

    Access Row and Column Ranges

    Use the following members to access multiple adjacent rows or columns as a RowRange or ColumnRange object:

    Task Rows Columns
    Access a range by zero-based indexes RowCollection.Item(firstIndex, lastIndex) ColumnCollection.Item(firstIndex, lastIndex)
    Access a range by reference (for example, 2:5 or B:D) RowCollection.GetRange(reference) ColumnCollection.GetRange(reference)
    Access rows or columns that contain a cell range CellRange.EntireRows CellRange.EntireColumns

    RowRange and ColumnRange members specify height or width, visibility, and the cell style for all rows or columns in the range. These members also fit rows or columns to cell content and clear cells.

    The following code snippet specifies the width of columns B:E, applies the Heading 1 style to rows 1:2, and hides rows 10:20:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    using var workbook = new Workbook();
    var worksheet = workbook.Worksheets[0];
    
    // Specify the width of columns B through E.
    worksheet.Columns.GetRange("B:E").SetWidth(15);
    
    // Apply the Heading 1 style to rows 1 and 2 (indexes 0 and 1).
    worksheet.Rows[0, 1].StyleName = PredefinedStyleNames.Heading1;
    
    // Hide rows 10 through 20.
    worksheet.Rows.GetRange("10:20").Visible = false;
    
    Directory.CreateDirectory("output");
    using var stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Insert Rows and Columns

    Use the Worksheet.InsertRows and Worksheet.InsertColumns methods to insert rows and columns at a specified position. The count parameter specifies the number of rows or columns to insert.

    The following code snippet inserts one row at index 2 and three columns starting at index 4:

    Worksheet worksheet = workbook.Worksheets[0];
    
    // Insert one row at index 2.
    worksheet.InsertRows(2);
    
    // Insert three columns starting at index 4.
    worksheet.InsertColumns(4, 3);
    

    Inserted rows shift existing cells down. Inserted columns shift existing cells to the right.

    The format parameter specifies the format of inserted rows or columns. The following InsertFormatMode values are available:

    Value Description
    SameAsPrevious Applies the format of the preceding row or column. This is the default setting.
    SameAsNext Applies the format of the following row or column.
    None Does not apply the format of adjacent rows or columns.

    The following code snippet inserts a row with the same formatting as that of the following row:

    Worksheet worksheet = workbook.Worksheets[0];
    
    // Insert a row at index 5.
    // The new row uses the format of row 6.
    worksheet.InsertRows(5, 1, InsertFormatMode.SameAsNext);
    

    Delete Rows and Columns

    Use Worksheet.DeleteRows and Worksheet.DeleteColumns methods to delete rows and columns at a specified position. The count parameter specifies the number of rows or columns to delete.

    The following code snippet deletes one row at index 2 and three columns starting at index 4:

    Worksheet worksheet = workbook.Worksheets[0];
    
    // Delete one row at index 2.
    worksheet.DeleteRows(2);
    
    // Delete three columns starting at index 4.
    worksheet.DeleteColumns(4, 3);
    

    Note

    Insert and delete operations do not change worksheet dimensions. The worksheet always supports up to 1,048,576 rows and 16,384 columns.

    See Also