You can bind a range of cells using the CellRange property in the SpreadDataSource class. This process creates a data source of the cell range using the SpreadDataSource control. This data source can then be bound to other controls (such as a chart or list box). You can put the initial data in the Spread component by binding it to a data source or leaving the component unbound and putting the data in the cells with code or other means (such as typing in the cells). If the component is unbound, then the DataTextField property would be the same as the column header text.
The SpreadDataSource control can also be added to the Toolbox in Visual Studio.
There are many alternative ways to set up data binding. To learn more about data binding in Visual Studio .NET, consult the Visual Studio .NET documentation.
Return to the overview Managing Data Binding.
Using the Properties Window
- Add the SpreadDataSource control to the form (double-click the icon in the toolbox).
- Set the CellRange property in the properties window.
- Set the sheet name.
- Set the SpreadID property.
- Set the DataSource property of the control you want to bind to.
Using Code
- Create a Spread data source object.
- Set the CellRange and other properties for the SpreadDataSource class.
- Add the Spread data source to the page.
Example
This example binds the Spread component to a data source, creates a cell range data source, and then binds the cell range data source to a list box control.
C# | Copy Code |
---|---|
// Create a data model using a data source. FpSpread1.DataSource = AccessDataSource1; FarPoint.Web.Spread.SpreadDataSource spreadDS = new FarPoint.Web.Spread.SpreadDataSource(); spreadDS.SheetName = FpSpread1.ActiveSheetView.SheetName; spreadDS.SpreadID = "FpSpread1";//FpSpread1.ID; spreadDS.CellRange = new FarPoint.Web.Spread.Model.CellRange(0, 0, 3, 1); this.Controls.Add(spreadDS); this.ListBox1.DataSource = spreadDS; this.ListBox1.DataTextField = "UserName"; this.ListBox1.DataBind(); |
VB | Copy Code |
---|---|
' Create a data model using a data source. FpSpread1.DataSource = AccessDataSource1 Dim spreadDS As New FarPoint.Web.Spread.SpreadDataSource() spreadDS.SheetName = FpSpread1.ActiveSheetView.SheetName spreadDS.SpreadID = "FpSpread1" spreadDS.CellRange = New FarPoint.Web.Spread.Model.CellRange(0, 0, 3, 1) Controls.Add(spreadDS) ListBox1.DataSource = spreadDS ListBox1.DataTextField = "UserName" ListBox1.DataBind() |
If your data set is not updated when you click the Update button, make sure that you have code similar to this in your page load event so that you only re-create the bound data when you are loading for the first time and not overwriting it on each post back.
C# | Copy Code |
---|---|
if !(Page.IsPostBack)
{
FpSpread1.DataBind();
}
|
VB | Copy Code |
---|---|
If Not Page.IsPostBack Then FpSpread1.DataBind() End If |