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
  • Read and Write Cell Values

    • 3 minutes to read

    Use the Cell.Value property to read and write cell values. This property returns a CellValue object that specifies the cell’s content. You can assign text, numeric, Boolean, or error values to it. The CellValue.Type property indicates the type of the stored value.

    using DevExpress.Docs.Spreadsheet;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.Cells[0, 0].Value = "Product";
    worksheet.Cells[0, 1].Value = 1250.50;
    worksheet.Cells[1, 0].Value = true;
    worksheet.Cells[1, 1].Value = CellValue.FromError(SpreadsheetError.NotAvailable);
    

    For information on how to access cells and cell ranges in a worksheet, refer to the following help topic: Access Cells and Cell Ranges.

    Specify Typed Cell Values

    Use the following methods to implicitly assign typed values and avoid type conversion operations:

    The following code snippet creates typed values and assigns them to cells:

    using DevExpress.Docs.Spreadsheet;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    worksheet.Cells[0, 0].Value = CellValue.FromNumeric(1250.50);
    worksheet.Cells[0, 1].Value = CellValue.FromText("Product");
    worksheet.Cells[1, 0].Value = CellValue.FromBoolean(true);
    worksheet.Cells[1, 1].Value = CellValue.FromError(SpreadsheetError.NotAvailable);
    

    Obtain Typed Cell Values

    To retrieve a value of a specific type from a cell, use the following properties:

    var cell00 = worksheet.Cells[0, 0].Value.NumericValue;
    var cell01 = worksheet.Cells[0, 1].Value.TextValue;
    var cell02 = worksheet.Cells[0, 2].Value.BooleanValue;
    var cell03 = worksheet.Cells[0, 3].Value.ErrorValue;
    

    Note: If the cell value’s actual type does not match the requested value type, an InvalidOperationException is thrown.

    Assign a Formula

    Assigning a value to Cell.Value removes the cell formula. Assign a formula to Cell.Formula to calculate a value instead. To calculate the formula and update the cell value, call the Calculate method.

    For information about how to work with formulas in workbooks, refer to the following section: Create Formulas and Use Cell References.

    Read and Write Range Values

    The CellRange.Value property returns a value only when all cells in the range contain the same value. The property returns null when cells contain different values. Assign a CellValue to set the value for every cell in the range. Assign null or CellValue.Empty to clear values in the range.

    The following example assigns the same value to every cell in a range:

    worksheet.GetRange(0,0,5,5).Value = "Pending";
    

    Clear Cell Value

    To clear a cell’s value, assign CellValue.Empty or Null to the cell’s Value property.

    worksheet.Cells[0, 0].Value = CellValue.Empty;
    
    See Also