Skip to main content
Row

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

DataSourceOptionsBase.CellValueConverter Property

Gets or sets the converter that transforms cell values into custom objects and back.

Namespace: DevExpress.Spreadsheet

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

NuGet Package: DevExpress.Spreadsheet.Core

#Declaration

public IBindingRangeValueConverter CellValueConverter { get; set; }

#Property Value

Type Description
IBindingRangeValueConverter

An object implementing the IBindingRangeValueConverter interface which provides methods for converting cell values into required data types and back.

#Example

This code snippet contains the code of the converter which provides the IBindingRangeValueConverter.TryConvertFromObject method used to convert fields of a custom WeatherReport object for proper display in a worksheet and the IBindingRangeValueConverter.ConvertToObject method for storing cell values in the fields of a custom object.

View Example

public class MyWeatherConverter : IBindingRangeValueConverter {
    public object ConvertToObject(CellValue value, Type requiredType, int columnIndex) {
        if (requiredType == typeof(DateTime))
            return value.DateTimeValue;
        if (requiredType == typeof(Weather)) {
            if (requiredType == typeof(Weather)) {
                Weather w;
                if (Enum.TryParse(value.TextValue, out w)) return w;
                return Weather.Undefined;
            }
            else
                return value.TextValue;
        }
        if (requiredType == typeof(List<HourlyReport>))
            return new List<HourlyReport>();
        return value.TextValue;
    }
    public CellValue TryConvertFromObject(object value) {
        if (value is DateTime) {
            return ((DateTime)value).ToString("MMM-dd");
        }
        if (value is Weather) {
            return value.ToString();
        }
        if (value is List<HourlyReport>) {
            var hourly = (List<HourlyReport>)value;
            if (hourly.Count == 0) return "Undefined";
            var high = hourly
                .OrderByDescending(p => p.Temperature)
                .FirstOrDefault()
                .Temperature;
            var low = hourly
                .OrderBy(p => p.Temperature)
                .FirstOrDefault()
                .Temperature;
            return String.Format("High - {0}, Low - {1}", high, low);
        }

        return CellValue.TryCreateFromObject(value);
    }
}
public class WeatherReport {
    [DisplayName("Date")]
    public DateTime Date { get; set; }
    [DisplayName("Weather Condition")]
    public Weather Weather { get; set; }
    [DisplayName("Max and Min Temperature")]
    public List<HourlyReport> HourlyReport { get; set; }
}

public class HourlyReport {
    public int Hour { get; set; }
    public int Temperature { get; set; }
}

public enum Weather {
    Sunny,
    Cloudy,
    Windy,
    Gloomy,
    Foggy,
    Misty,
    Rainy,
    Undefined
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the CellValueConverter property.

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.

See Also