Back to Devexpress

WorksheetDataBindingCollection.BindTableToDataSource(Object, CellRange, ExternalDataSourceOptions) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheetdatabindingcollection-dot-bindtabletodatasource-x28-system-dot-object-devexpress-dot-spreadsheet-dot-cellrange-devexpress-dot-spreadsheet-dot-externaldatasourceoptions-x29.md

latest7.9 KB
Original Source

WorksheetDataBindingCollection.BindTableToDataSource(Object, CellRange, ExternalDataSourceOptions) Method

Creates a worksheet table from the specified range and binds it to the data source using the specified options.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
WorksheetTableDataBinding BindTableToDataSource(
    object dataSource,
    CellRange range,
    ExternalDataSourceOptions options
)
vb
Function BindTableToDataSource(
    dataSource As Object,
    range As CellRange,
    options As ExternalDataSourceOptions
) As WorksheetTableDataBinding

Parameters

NameTypeDescription
dataSourceObject

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

| | range | CellRange |

A CellRange object specifying the range of the worksheet table being created.

| | options | ExternalDataSourceOptions |

An ExternalDataSourceOptions object that contains options used to establish binding.

|

Returns

TypeDescription
WorksheetTableDataBinding

A WorksheetTableDataBinding object that contains information about the data-bound worksheet table.

|

Example

View Example

csharp
private void BindWeatherReportToTable(object weatherDatasource, CellRange bindingRange) {
    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 {
        WorksheetTableDataBinding sheetDataBinding = sheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions);
        sheetDataBinding.Table.Style = spreadsheetControl1.Document.TableStyles[BuiltInTableStyleId.TableStyleMedium14];

        // Adjust the column width.
        sheetDataBinding.Range.AutoFitColumns();
    }
    catch(Exception e) {
        MessageBox.Show(e.Message, "Binding Exception");
    }
}
vb
Private Sub BindWeatherReportToTable(ByVal weatherDatasource As Object, ByVal bindingRange 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 sheetDataBinding As WorksheetTableDataBinding = sheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions)
        sheetDataBinding.Table.Style = spreadsheetControl1.Document.TableStyles(BuiltInTableStyleId.TableStyleMedium14)

        ' Adjust the column width.
        sheetDataBinding.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 BindTableToDataSource(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#L76

csharp
try {
    WorksheetTableDataBinding sheetDataBinding = sheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions);
    sheetDataBinding.Table.Style = spreadsheetControl1.Document.TableStyles[BuiltInTableStyleId.TableStyleMedium14];

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

csharp
{
    WorksheetTableDataBinding sheetDataBinding = worksheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions);
    sheetDataBinding.Table.Style = worksheet.Workbook.TableStyles[BuiltInTableStyleId.TableStyleMedium14];

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

vb
Try
    Dim sheetDataBinding As WorksheetTableDataBinding = sheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions)
    sheetDataBinding.Table.Style = spreadsheetControl1.Document.TableStyles(BuiltInTableStyleId.TableStyleMedium14)

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

vb
Try
    Dim sheetDataBinding As WorksheetTableDataBinding = worksheet.DataBindings.BindTableToDataSource(weatherDatasource, bindingRange, dsOptions)
    sheetDataBinding.Table.Style = worksheet.Workbook.TableStyles(BuiltInTableStyleId.TableStyleMedium14)

See Also

Data Binding in WinForms Spreadsheet Control

Table

WorksheetTableDataBinding

DataBindings

Error

WorksheetDataBindingCollection Interface

WorksheetDataBindingCollection Members

DevExpress.Spreadsheet Namespace