Back to Devexpress

WorksheetDataBindingCollection.BindToDataSource(Object, Int32, Int32, ExternalDataSourceOptions) Method

officefileapi-devexpress-dot-spreadsheet-dot-worksheetdatabindingcollection-dot-bindtodatasource-x28-system-dot-object-system-dot-int32-system-dot-int32-devexpress-dot-spreadsheet-dot-externaldatasourceoptions-x29.md

latest7.0 KB
Original Source

WorksheetDataBindingCollection.BindToDataSource(Object, Int32, Int32, ExternalDataSourceOptions) Method

Binds the worksheet cell range to the specified data source using the specified options.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
WorksheetDataBinding BindToDataSource(
    object dataSource,
    int rowIndex,
    int columnIndex,
    ExternalDataSourceOptions options
)
vb
Function BindToDataSource(
    dataSource As Object,
    rowIndex As Integer,
    columnIndex As Integer,
    options As ExternalDataSourceOptions
) As WorksheetDataBinding

Parameters

NameTypeDescription
dataSourceObject

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

| | rowIndex | Int32 |

An integer that is the row index of the top left cell in the binding range.

| | columnIndex | Int32 |

An integer that is the column index of the top left cell in the binding range.

| | options | ExternalDataSourceOptions |

An ExternalDataSourceOptions object that contains options used to establish binding.

|

Returns

TypeDescription
WorksheetDataBinding

A WorksheetDataBinding object that contains information about the data-bound range.

|

Example

View Example

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

    // Check for range conflicts.
    var dataBindingConflicts = sheet.DataBindings.
        Where(d => (d.Range.RightColumnIndex >= bindingRange.LeftColumnIndex) || (d.Range.BottomRowIndex >= bindingRange.TopRowIndex)) ;
    if (dataBindingConflicts.Count() > 0) {
        MessageBox.Show("Cannot bind the range to data.\r\nThe worksheet contains other binding ranges which may conflict.", "Range Conflict");
        return;
    }

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

    // Bind the data source to the worksheet range.
    WorksheetDataBinding sheetDataBinding = sheet.DataBindings.BindToDataSource(weatherDatasource, bindingRange, dsOptions);

    // Adjust the column width.
    sheetDataBinding.Range.AutoFitColumns();
}
vb
Private Sub BindWeatherReportToRange(ByVal weatherDatasource As Object, ByVal bindingRange As CellRange)
    Dim sheet As Worksheet = spreadsheetControl1.Document.Worksheets(0)

    ' Check for range conflicts.
    Dim dataBindingConflicts = sheet.DataBindings.Where(Function(d) (d.Range.RightColumnIndex >= bindingRange.LeftColumnIndex) OrElse (d.Range.BottomRowIndex >= bindingRange.TopRowIndex))
    If dataBindingConflicts.Count() > 0 Then
        MessageBox.Show("Cannot bind the range to data." & ControlChars.CrLf & "The worksheet contains other binding ranges which may conflict.", "Range Conflict")
        Return
    End If

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

    ' Bind the data source to the worksheet range.
    Dim sheetDataBinding As WorksheetDataBinding = sheet.DataBindings.BindToDataSource(weatherDatasource, bindingRange, dsOptions)

    ' Adjust the column width.
    sheetDataBinding.Range.AutoFitColumns()
End Sub

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

wpf-spreadsheet-bind-a-worksheet-to-generic-list-or-bindinglist-data-source/CS/DataBindingToListExample/MainWindow.xaml.cs#L57

csharp
Worksheet sheet = spreadsheetControl1.Document.Worksheets[0];
WorksheetDataBinding sheetDataBinding = sheet.DataBindings.BindToDataSource(weatherDatasource, 2, 1, dsOptions);
#endregion #BindTheList

wpf-spreadsheet-bind-a-worksheet-to-generic-list-or-bindinglist-data-source/VB/DataBindingToListExample/MainWindow.xaml.vb#L60

vb
Dim sheet As Worksheet = spreadsheetControl1.Document.Worksheets(0)
            Dim sheetDataBinding As WorksheetDataBinding = sheet.DataBindings.BindToDataSource(weatherDatasource, 2, 1, dsOptions)
' #End Region ' #BindTheList

See Also

Data Binding in WinForms Spreadsheet Control

DataBindings

Error

WorksheetDataBindingCollection Interface

WorksheetDataBindingCollection Members

DevExpress.Spreadsheet Namespace