Back to Devexpress

CellValue.FromObject(Object) Method

officefileapi-devexpress-dot-spreadsheet-dot-cellvalue-dot-fromobject-x28-system-dot-object-x29.md

latest9.1 KB
Original Source

CellValue.FromObject(Object) Method

Converts the specified object to a cell value using the default converter.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public static CellValue FromObject(
    object value
)
vb
Public Shared Function FromObject(
    value As Object
) As CellValue

Parameters

NameTypeDescription
valueObject

An object to be converted to a CellValue.

|

Returns

TypeDescription
CellValue

A CellValue object. If the conversion cannot be performed, an InvalidCastException is thrown.

|

Remarks

To convert a cell value to an object, use the CellValue.ToObject method. For more information on cell values and their types, refer to Cell Data Types.

Example

The CellRange.SetValue and CellValue.FromObject methods allow you to convert objects of different types to SpreadsheetControl-compatible cell values. The CellValue.ToObject method performs an inverse operation - it gets objects from cell values.

The code below demonstrates how to convert cell values of different types to objects and add them to an array, and convert array elements to CellValue objects and assign them to cells. In this example, the default cell value converter is used. However, you can create your own custom converter that implements the ICellValueConverter interface and use it for conversion.

View Example

csharp
// Add data of different types to cells of the range.
CellRange sourceRange = worksheet["B1:B3"];
sourceRange[0].Value = "Text";
sourceRange[1].Formula = "=PI()";
sourceRange[2].Value = DateTime.Now;
sourceRange[2].NumberFormat = "d-mmm-yy";

// Get the number of cells in the range.
int cellCount = sourceRange.RowCount * sourceRange.ColumnCount;

// Declare an array to store elements of different types.
object[] array = new object[cellCount];

// Convert cell values to objects and add them to the array.
for (int i = 0; i < cellCount; i++) {
    array[i] = sourceRange[i].Value.ToObject();
}

// Convert array elements to cell values and assign them to cells in the fifth row. 
for (int i = 0; i < array.Length; i++) {
    worksheet.Rows["5"][i + 1].SetValue(array[i]);
    // An alternative way to do this is to use the CellValue.FromObject method.
    // worksheet.Rows["5"][i+1].Value = CellValue.FromObject(array[i]);
}
vb
' Add data of different types to cells of the range.
Dim sourceRange As CellRange = worksheet("B1:B3")
sourceRange(0).Value = "Text"
sourceRange(1).Formula = "=PI()"
sourceRange(2).Value = Date.Now
sourceRange(2).NumberFormat = "d-mmm-yy"

' Get the number of cells in the range.
Dim cellCount As Integer = sourceRange.RowCount * sourceRange.ColumnCount

' Declare an array to store elements of different types.
Dim array(cellCount - 1) As Object

' Convert cell values to objects and add them to the array.
For i As Integer = 0 To cellCount - 1
    array(i) = sourceRange(i).Value.ToObject()
Next i

' Convert array elements to cell values and assign them to cells in the fifth row. 
For i As Integer = 0 To array.Length - 1
    worksheet.Rows("5")(i + 1).SetValue(array(i))
    ' An alternative way to do this is to use the CellValue.FromObject method.
    ' worksheet.Rows["5"][i+1].Value = CellValue.FromObject(array[i]);
Next i

The following code snippets (auto-collected from DevExpress Examples) contain references to the FromObject(Object) 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-spreadsheet-control-create-a-data-entry-form/CS/DataEntryFormSample/SpreadsheetBindingManager.cs#L207

csharp
if (cellBindings.TryGetValue(propertyDescriptor.Name, out reference))
        SetCellValue(reference, CellValue.FromObject(propertyDescriptor.GetValue(currentItem)));
}

wpf-spreadsheet-control-create-a-data-entry-form/CS/WpfDataEntryFormSample/SpreadsheetBindingManager.cs#L195

csharp
if (cellBindings.TryGetValue(propertyDescriptor.Name, out reference))
        SetCellValue(reference, CellValue.FromObject(propertyDescriptor.GetValue(currentItem)));
}

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/ImportActions.cs#L142

csharp
else
    result = CellValue.FromObject(value);
return true;

winforms-spreadsheet-control-create-a-data-entry-form/VB/DataEntryFormSample/SpreadsheetBindingManager.vb#L179

vb
If cellBindings.TryGetValue(propertyDescriptor.Name, reference) Then
    SetCellValue(reference, CellValue.FromObject(propertyDescriptor.GetValue(currentItem)))
End If

wpf-spreadsheet-control-create-a-data-entry-form/VB/WpfDataEntryFormSample/SpreadsheetBindingManager.vb#L176

vb
If cellBindings.TryGetValue(propertyDescriptor.Name, reference) Then
    SetCellValue(reference, CellValue.FromObject(propertyDescriptor.GetValue(currentItem)))
End If

how-to-use-excel-add-ins-in-winforms-spreadsheet/VB/SpreadsheetAddIn/Form1.vb#L222

vb
End If
    Return CellValue.FromObject(value)
End Function

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/ImportActions.vb#L132

vb
Else
    result = CellValue.FromObject(value)
End If

See Also

TryCreateFromObject

ToObject

SetValue(Object)

Value

Cell Data Types

How to: Change a Cell or Cell Range Value

How to: Convert Objects to Cell Values and Cell Values to Objects

CellValue Class

CellValue Members

DevExpress.Spreadsheet Namespace