Back to Devexpress

DataImportOptions.Converter Property

officefileapi-devexpress-dot-spreadsheet-dot-dataimportoptions.md

latest5.1 KB
Original Source

DataImportOptions.Converter Property

Gets or sets a converter for imported data.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public IDataValueConverter Converter { get; set; }
vb
Public Property Converter As IDataValueConverter

Property Value

TypeDescription
IDataValueConverter

An object implementing the IDataValueConverter interface.

|

Remarks

The following example demonstrates using a custom converter in the WorksheetExtensions.Import method.

Example

View Example

csharp
List<TestObject> list = new List<TestObject>();
list.Add(new TestObject(1, "1", true));
list.Add(new TestObject(2, "2", false));
worksheet.Import(list, 0, 0, new DataSourceImportOptions() { Converter = new TestDataValueConverter() });
vb
Dim list As New List(Of TestObject)()
list.Add(New TestObject(1, "1", True))
list.Add(New TestObject(2, "2", False))
worksheet.Import(list, 0, 0, New DataSourceImportOptions() With {.Converter = New TestDataValueConverter()})
csharp
class TestDataValueConverter : DevExpress.Spreadsheet.IDataValueConverter
{
    public bool TryConvert(object value, int columnIndex, out DevExpress.Spreadsheet.CellValue result)
    {
        string strValue = value as string;
        if (strValue != null)
        {
            int str2int;
            bool success = Int32.TryParse(strValue, out str2int);
            result = success ? str2int : 0;
            return true;
        }
        Type valueType = value.GetType();
        if (valueType == typeof(int))
            result = (int)value;
        else
            result = null;
        return true;
    }
}
vb
Friend Class TestDataValueConverter
    Implements DevExpress.Spreadsheet.IDataValueConverter

    Public Function TryConvert(ByVal value As Object, ByVal columnIndex As Integer,  ByRef result As DevExpress.Spreadsheet.CellValue) As Boolean Implements DevExpress.Spreadsheet.IDataValueConverter.TryConvert
        Dim strValue As String = TryCast(value, String)
        If strValue IsNot Nothing Then
            Dim str2int As Integer = Nothing
            Dim success As Boolean = Int32.TryParse(strValue, str2int)
            result = If(success, str2int, 0)
            Return True
        End If
        Dim valueType As Type = value.GetType()
        If valueType Is GetType(Integer) Then
            result = DirectCast(value, Integer)
        Else
            result = Nothing
        End If
        Return True
    End Function
End Class
csharp
class TestObject
{
    public TestObject(int intValue, string value, bool boolValue)
    {
        this.intValue = intValue;
        this.Value = value;
        this.BoolValue = boolValue;

    }
    public int intValue;
    private int privateValue { get { return 123; } }
    public int IntValue { get { return intValue + privateValue - 123; } }
    public string Value { get; set; }
    public bool BoolValue { get; set; }
    public int this[int index] { get { return index; } }
}
vb
Friend Class TestObject
    Public Sub New(ByVal intValue As Integer, ByVal value As String, ByVal boolValue As Boolean)
        Me.intValue_Renamed = intValue
        Me.Value = value
        Me.BoolValue = boolValue

    End Sub
    Public intValue_Renamed As Integer
    Private ReadOnly Property privateValue() As Integer
        Get
            Return 123
        End Get
    End Property
    Public ReadOnly Property IntValue() As Integer
        Get
            Return intValue_Renamed + privateValue - 123
        End Get
    End Property
    Public Property Value() As String
    Public Property BoolValue() As Boolean
    Default Public ReadOnly Property Item(ByVal index As Integer) As Integer
        Get
            Return index
        End Get
    End Property
End Class

See Also

DataImportOptions Class

DataImportOptions Members

DevExpress.Spreadsheet Namespace