Back to Devexpress

How to: Bind a Data Grid to a Cell Range

windowsforms-118086-controls-and-libraries-spreadsheet-examples-data-binding-how-to-bind-a-data-grid-to-a-cell-range.md

latest2.2 KB
Original Source

How to: Bind a Data Grid to a Cell Range

  • Jan 19, 2024

This example demonstrates how to bind the GridControl to a table in a worksheet. For this, use the CellRange.GetDataSource method to create a data source object from the cell range with table data (Table.DataRange) and then assign it to the GridControl.DataSource property.

The two-way binding is supported: any changes to table data (such as editing values, inserting and deleting rows, sorting and filtering) are immediately propagated to the Data Grid, and data edited in the grid is reflected in the table.

csharp
IWorkbook workbook = spreadsheet.Document;
Worksheet worksheet = workbook.Worksheets[0];
// Access the table on the worksheet. 
Table expensesTable = worksheet.Tables[0];
// Specify the data source settings.
RangeDataSourceOptions options = new RangeDataSourceOptions();
options.PreserveFormulas = true;
options.SkipHiddenRows = true;
// Bind the grid control to the table data.
gridControl1.DataSource = expensesTable.DataRange.GetDataSource(options);
vb
Dim workbook As IWorkbook = spreadsheet.Document
Dim worksheet As Worksheet = workbook.Worksheets(0)
' Access the table on the worksheet. 
Dim expensesTable As Table = worksheet.Tables(0)
' Specify the data source settings.
Dim options As New RangeDataSourceOptions()
options.PreserveFormulas = True
options.SkipHiddenRows = True
' Bind the grid control to the table data.
gridControl1.DataSource = expensesTable.DataRange.GetDataSource(options)

The following image shows the resulting application.

View Example: Use Spreadsheet as Data Source