Spread for WPF provides data binding support that makes it easy to populate the data in worksheets with minimal code. By binding a worksheet to a data source, you can automatically display data from various sources, like data tables or data collections.
This topic contains how to bind worksheets to different data sources and additional new rows or unbound columns.
Spread for WPF allows you to bind the worksheets to several types of data sources, as mentioned below:
Spread for WPF also allows you to bind a table to a data source. For more information about table binding, you can refer to the Table Binding topic.
To bind a worksheet to a data table, follow the steps below:
To bind a worksheet to the DataView, bind a data view created from the dataset using the DataSource property of the IDataManager interface.
To bind a worksheet to a CollectionView, follow the steps below.
You can use the DataAllowAddNew property of the IDataManager interface to allow users to add a new row to a bound sheet at runtime. When this property is set to true, an empty row appears at the end of the worksheet for users to enter new data. When data is entered, it becomes a bound row and is updated in the data source and a new empty row appears automatically at the end.
Note:
The following example code will add an unbound row to a bound worksheet at run time. You can add an unbound row using both the XAML view and the code view.
After binding a worksheet to a data source, you may want to add an unbound row to include additional data. This new unbound row can also be added back to the data source. Furthermore, you can create a new row at runtime and bind it to the data source. To add an unbound row to a bound worksheet, use the AddUnboundRows method from the IDataManager interface. Additionally, to include that unbound row in the data source, use the AddRowToDataSource method of the IDataManager interface.
Follow the steps below to add an unbound row and bind it to a data source:
The below code adds an unbound row to a bound worksheet and adds the unbound row to the data source.
When your worksheet is bound to a data source, you can also add unbound columns to the data source and insert them at any position. There are two ways to add/insert an unbound column to a worksheet.
The following sample code automatically generates columns using data binding and then adds an unbound column "Total Amount" to the worksheet having the formula "Quantity * UnitPrice".