How to: Bind a Data Grid to a Cell Range
- 2 minutes to read
This example demonstrates how to bind the GridControl to a table in a worksheet. For this, use the Range.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.
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-bind-a-grid-control-to-the-worksheet-data-t461090
IWorkbook workbook = spreadsheet.Document; Worksheet worksheet = workbook.Worksheets; // Access the table on the worksheet. Table expensesTable = worksheet.Tables; // 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);
The following image shows the resulting application.
See Also Other examples demonstrating how to use a cell range as a data source for various data-aware controls are available in the DevExpress Code Examples database at http://www.devexpress.com/example=T483776.