officefileapi-devexpress-dot-spreadsheet-dot-cellvalue-dot-fromobject-x28-system-dot-object-devexpress-dot-spreadsheet-dot-icellvalueconverter-x29.md
Converts the specified object to a cell value using the custom converter.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
public static CellValue FromObject(
object value,
ICellValueConverter converter
)
Public Shared Function FromObject(
value As Object,
converter As ICellValueConverter
) As CellValue
| Name | Type | Description |
|---|---|---|
| value | Object |
An object to be converted to a CellValue.
| | converter | ICellValueConverter |
A custom converter object that implements the ICellValueConverter interface.
|
| Type | Description |
|---|---|
| CellValue |
A CellValue object. If the conversion cannot be performed, an InvalidCastException is thrown.
|
To convert a cell value to object, use the CellValue.ToObject method. For more information on cell values and their types, refer to Cell Data Types.
To convert custom objects to cell values and vise versa, you can apply your own converters.
This example demonstrates how to convert a color object (Color) to a SpreadsheetControl-compatible cell value of the text type (CellValue) that corresponds to the color name. To do this, create a custom converter class that implements the ICellValueConverter interface, and call the CellValue.FromObject method with the color object and custom converter passed as parameters.
Worksheet worksheet = workbook.Worksheets[0];
Cell cell = worksheet.Cells["A1"];
cell.FillColor = Color.Orange;
cell.Value = CellValue.FromObject(cell.FillColor, new ColorToNameConverter());
// ...
class ColorToNameConverter : ICellValueConverter {
object ICellValueConverter.ConvertToObject(CellValue value) {
return null;
}
CellValue ICellValueConverter.TryConvertFromObject(object value) {
bool isColor = value.GetType() == typeof(Color);
if (!isColor)
return null;
return ((Color)value).Name;
}
}
Dim worksheet As Worksheet = workbook.Worksheets(0)
Dim cell As Cell = worksheet.Cells("A1")
cell.FillColor = Color.Orange
cell.Value = CellValue.FromObject(cell.FillColor, New ColorToNameConverter())
' ...
Private Class ColorToNameConverter
Implements ICellValueConverter
Private Function ICellValueConverter_ConvertToObject(ByVal value As CellValue) As Object Implements ICellValueConverter.ConvertToObject
Return Nothing
End Function
Private Function ICellValueConverter_TryConvertFromObject(ByVal value As Object) As CellValue Implements ICellValueConverter.TryConvertFromObject
Dim isColor As Boolean = value.GetType() Is GetType(Color)
If Not isColor Then
Return Nothing
End If
Return DirectCast(value, Color).Name
End Function
End Class
The following code snippets (auto-collected from DevExpress Examples) contain references to the FromObject(Object, ICellValueConverter) 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-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/CellActions.cs#L240
cell.FillColor = Color.Orange;
cell.Value = CellValue.FromObject(cell.FillColor, new ColorToNameConverter());
// ...
cell.FillColor = Color.Orange;
cell.Value = CellValue.FromObject(cell.FillColor, new ColorToNameConverter());
// ...
winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/CellActions.vb#L220
cell.FillColor = Color.Orange
cell.Value = CellValue.FromObject(cell.FillColor, New ColorToNameConverter())
' ...
cell.FillColor = Color.Orange
cell.Value = CellValue.FromObject(cell.FillColor, New ColorToNameConverter())
' ...
See Also
How to: Change a Cell or Cell Range Value
How to: Convert Objects to Cell Values and Cell Values to Objects