CellValue.ToObject(ICellValueConverter) Method
Converts the cell value to an object using the specified converter.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Spreadsheet.v24.1.Core.dll
NuGet Package: DevExpress.Spreadsheet.Core
Declaration
Parameters
Name | Type | Description |
---|---|---|
converter | ICellValueConverter | A custom converter object that implements the ICellValueConverter interface. |
Returns
Type | Description |
---|---|
Object | A Object converted from the cell value. |
Remarks
To convert an object to a cell value, use the CellValue.FromObject 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.
// 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]);
}