Skip to main content
Row

TableCollection.Add(Object, CellRange, ExternalDataSourceOptions) Method

Creates a table from the specified range, adds it to the collection and binds it to the data source.

Namespace: DevExpress.Spreadsheet

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

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

Table Add(
    object dataSource,
    CellRange range,
    ExternalDataSourceOptions options
)

Parameters

Name Type Description
dataSource Object

A data source object whose data should be retrieved in the worksheet.

range CellRange

A CellRange used to create a table.

options ExternalDataSourceOptions

An ExternalDataSourceOptions object that contains options used to establish binding.

Returns

Type Description
Table

A Table object that is the newly created table.

Remarks

The following rules are used, depending on the dimensions of the specified range:

Range Result
A single cell. A table is created to include all the data of the data source. It has the number of columns equal to the number of data fields, and the number of rows equal to the number of data rows in the data source. The top left corner of the table is the specified cell.
A range with a number of columns less than the number of data fields in the data source. An WorksheetDataBindingCollection.Error event occurs that indicates a column number mismatch. The table is not created.
A range with the number of columns equal to the number of data fields in the data source. A table is created with the number of rows equal to the number of data rows in the data source.
A range with a number of columns greater than the number of data fields in the data source A table is created that includes all columns from the data source and a number of empty columns to fill the entire range. The table has a number of rows equal to the number of data rows in the data source.

Example

View Example

private void BindWeatherReportToFixedTable(object weatherDatasource, CellRange selection) {
    Worksheet sheet = spreadsheetControl1.Document.Worksheets[0];

    // Remove all data bindings bound to the specified data source.
    sheet.DataBindings.Remove(weatherDatasource);

    // Specify the binding options.
    ExternalDataSourceOptions dsOptions = new ExternalDataSourceOptions();
    dsOptions.ImportHeaders = true;
    dsOptions.CellValueConverter = new MyWeatherConverter();
    dsOptions.SkipHiddenRows = true;

    // Create a table and bind the data source to the table.
    try {
        Table boundTable = sheet.Tables.Add(weatherDatasource, selection, dsOptions);
        boundTable.Style = spreadsheetControl1.Document.TableStyles[BuiltInTableStyleId.TableStyleMedium15];

        // Adjust the column width.
        boundTable.Range.AutoFitColumns();
    }
    catch (Exception e) {
        MessageBox.Show(e.Message, "Binding Exception");
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Add(Object, CellRange, ExternalDataSourceOptions) 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.

See Also