Back to Devexpress

DataTableExporter.CellValueConversionError Event

officefileapi-devexpress-dot-spreadsheet-dot-export-dot-datatableexporter-84ca8936.md

latest10.1 KB
Original Source

DataTableExporter.CellValueConversionError Event

Fires if an error occurs during conversion of a specific cell value to the value which should be stored in a data table.

Namespace : DevExpress.Spreadsheet.Export

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public event CellValueConversionErrorEventHandler CellValueConversionError
vb
Public Event CellValueConversionError As CellValueConversionErrorEventHandler

Event Data

The CellValueConversionError event's data class is CellValueConversionErrorEventArgs. The following properties provide information specific to this event:

PropertyDescription
ActionSpecifies the action performed after the DataTableExporter.CellValueConversionError event is handled.
CellIdentifies the cell containing the value that cannot be converted.
CellValueGets the value that caused the error.
ConversionResultGets an enumeration member that identifies the cause of the conversion error.
DataColumnProvides access to a column in a target DataTable that cannot be filled with data for the current row due to a conversion error.
DataTableValueGets or sets a value contained in the target DataTable.

The event data class exposes the following methods:

MethodDescription
Initialize(Cell, CellValue, DataColumn, ConversionResult)Initializes the object before passing it to a DataTableExporter.CellValueConversionError event handler.

Remarks

The following code illustrates how to handle the CellValueConversionError event.

View Example

csharp
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Export;
            Worksheet worksheet = spreadsheetControl1.Document.Worksheets[0];
            CellRange range = worksheet.Tables[0].Range;

            // Create a data table with column names obtained from the first row in a range.
            // Column data types are obtained from cell value types of cells in the first data row of the worksheet range.
            DataTable dataTable = worksheet.CreateDataTable(range, true);

            // Create the exporter that obtains data from the specified range which has a header row and populates the previously created data table. 
            DataTableExporter exporter = worksheet.CreateDataTableExporter(range, dataTable, true);
            // Handle value conversion errors.
            exporter.CellValueConversionError += exporter_CellValueConversionError;

            // Specify exporter options.
            exporter.Options.ConvertEmptyCells = true;
            exporter.Options.DefaultCellValueToColumnTypeConverter.EmptyCellValue = 0;
            exporter.Options.DefaultCellValueToColumnTypeConverter.SkipErrorValues = barCheckItemSkipErrors.Checked;

            // Perform the export.
            exporter.Export();
        void exporter_CellValueConversionError(object sender, CellValueConversionErrorEventArgs e)
        {
            MessageBox.Show("Error in cell " + e.Cell.GetReferenceA1());
            e.DataTableValue = null;
            e.Action = DataTableExporterAction.Continue;
        }
vb
Imports DevExpress.Spreadsheet
Imports DevExpress.Spreadsheet.Export
            Dim worksheet As Worksheet = spreadsheetControl1.Document.Worksheets(0)
            Dim range As CellRange = worksheet.Tables(0).Range

            ' Create a data table with column names obtained from the first row in a range.
            ' Column data types are obtained from cell value types of cells in the first data row of the worksheet range.
            Dim dataTable As DataTable = worksheet.CreateDataTable(range, True)

            ' Create the exporter that obtains data from the specified range which has a header row and populates the previously created data table. 
            Dim exporter As DataTableExporter = worksheet.CreateDataTableExporter(range, dataTable, True)
            ' Handle value conversion errors.
            AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError

            ' Specify exporter options.
            exporter.Options.ConvertEmptyCells = True
            exporter.Options.DefaultCellValueToColumnTypeConverter.EmptyCellValue = 0
            exporter.Options.DefaultCellValueToColumnTypeConverter.SkipErrorValues = barCheckItemSkipErrors.Checked

            ' Perform the export.
            exporter.Export()
        Private Sub exporter_CellValueConversionError(ByVal sender As Object, ByVal e As CellValueConversionErrorEventArgs)
            MessageBox.Show("Error in cell " & e.Cell.GetReferenceA1())
            e.DataTableValue = Nothing
            e.Action = DataTableExporterAction.Continue
        End Sub

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

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.

how-to-export-cell-range-to-a-datatable/CS/ExportToDataTableExample/Form1.cs#L55

csharp
// Handle value conversion errors.
exporter.CellValueConversionError += exporter_CellValueConversionError;

asp-net-mvc-grid-upload-and-display-excel-file/CS/UploadControlApplication/Models/HelperClass.cs#L18

csharp
DataTableExporter exporter = sheet.CreateDataTableExporter(range, table, false);
exporter.CellValueConversionError += exporter_CellValueConversionError;
exporter.Export();

asp-net-web-forms-grid-upload-and-display-excel-file/CS/Solution/Default.aspx.cs#L29

csharp
DataTableExporter exporter = sheet.CreateDataTableExporter(range, table, false);
exporter.CellValueConversionError += exporter_CellValueConversionError;
exporter.Export();

how-to-export-cell-range-to-a-datatable/VB/ExportToDataTableExample/Form1.vb#L54

vb
' Handle value conversion errors.
AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError
' Perform the export.

spreadsheet-document-api-export-worksheet-range-to-datatable/VB/ExportToDataTableWorkbookExample/Form1.vb#L32

vb
Dim exporter As DataTableExporter = worksheet.CreateDataTableExporter(range, dataTable, True)
AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError
Dim myconverter As New MyConverter()

asp-net-mvc-grid-upload-and-display-excel-file/VB/UploadControlApplication/Models/HelperClass.vb#L13

vb
Dim exporter As DataTableExporter = sheet.CreateDataTableExporter(range, table, False)
AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError
exporter.Export()

asp-net-web-forms-grid-upload-and-display-excel-file/VB/Solution/Default.aspx.vb#L35

vb
Dim exporter As DataTableExporter = sheet.CreateDataTableExporter(range, table, False)
AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError
exporter.Export()

See Also

DataTableExporter Class

DataTableExporter Members

DevExpress.Spreadsheet.Export Namespace