Back to Devexpress

TableCollection.Add(Object, CellRange, ExternalDataSourceOptions) Method

officefileapi-devexpress-dot-spreadsheet-dot-tablecollection-dot-add-x28-system-dot-object-devexpress-dot-spreadsheet-dot-cellrange-devexpress-dot-spreadsheet-dot-externaldatasourceoptions-x29.md

latest8.4 KB
Original Source

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.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
Table Add(
    object dataSource,
    CellRange range,
    ExternalDataSourceOptions options
)
vb
Function Add(
    dataSource As Object,
    range As CellRange,
    options As ExternalDataSourceOptions
) As Table

Parameters

NameTypeDescription
dataSourceObject

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

TypeDescription
Table

A Table object that is the newly created table.

|

Remarks

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

RangeResult
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 sourceA 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

csharp
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");
    }
}
vb
Private Sub BindWeatherReportToFixedTable(ByVal weatherDatasource As Object, ByVal selection As CellRange)
    Dim sheet As Worksheet = spreadsheetControl1.Document.Worksheets(0)

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

    ' Specify the binding options.
    Dim dsOptions As New ExternalDataSourceOptions()
    dsOptions.ImportHeaders = True
    dsOptions.CellValueConverter = New MyWeatherConverter()
    dsOptions.SkipHiddenRows = True

    ' Create a table and bind the data source to the table.
    Try
        Dim boundTable As Table = sheet.Tables.Add(weatherDatasource, selection, dsOptions)
        boundTable.Style = spreadsheetControl1.Document.TableStyles(BuiltInTableStyleId.TableStyleMedium15)

        ' Adjust the column width.
        boundTable.Range.AutoFitColumns()
    Catch e As Exception
        MessageBox.Show(e.Message, "Binding Exception")
    End Try
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references 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.

how-to-bind-a-worksheet-to-a-generic-list-or-a-bindinglist-data-source/CS/DataBindingToListExample/Form1.cs#L103

csharp
try {
    Table boundTable = sheet.Tables.Add(weatherDatasource, selection, dsOptions);
    boundTable.Style = spreadsheetControl1.Document.TableStyles[BuiltInTableStyleId.TableStyleMedium15];

spreadsheet-document-api-data-binding/CS/SpreadsheetApiDataBinding/Program.cs#L102

csharp
{
    Table boundTable = worksheet.Tables.Add(weatherDatasource, bindingRange, dsOptions);
    boundTable.Style = worksheet.Workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium15];

how-to-bind-a-worksheet-to-a-generic-list-or-a-bindinglist-data-source/VB/DataBindingToListExample/Form1.vb#L105

vb
Try
    Dim boundTable As Table = sheet.Tables.Add(weatherDatasource, selection, dsOptions)
    boundTable.Style = spreadsheetControl1.Document.TableStyles(BuiltInTableStyleId.TableStyleMedium15)

spreadsheet-document-api-data-binding/VB/SpreadsheetApiDataBinding/Program.vb#L90

vb
Try
    Dim boundTable As Table = worksheet.Tables.Add(weatherDatasource, bindingRange, dsOptions)
    boundTable.Style = worksheet.Workbook.TableStyles(BuiltInTableStyleId.TableStyleMedium15)

See Also

Data Binding in WinForms Spreadsheet Control

Table

WorksheetTableDataBinding

DataBindings

Error

BindTableToDataSource

TableCollection Interface

TableCollection Members

DevExpress.Spreadsheet Namespace