Spread for ASP.NET 12 Product Documentation
Binding to a Range
Spread for ASP.NET 12 Product Documentation > Developer's Guide > Managing Data Binding > Binding to a Range

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 be created with code or 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.

Using the Properties Window

  1. Add the SpreadDataSource control to the form (double-click the icon in the toolbox).
  2. Set the CellRange property in the properties window.
  3. Set the sheet name.
  4. Set the SpreadID property.
  5. Set the DataSource property of the control you want to bind to.

Using Code

  1. Create a data source.
  2. Set the Spread DataSource property to the data source.
  3. Create a Spread data source object.
  4. Set the CellRange and other properties for the SpreadDataSource class.
  5. Add the Spread data source to the page.
  6. Add a list box control 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
System.Data.DataTable dt = new System.Data.DataTable("Test");
System.Data.DataRow dr = default(System.Data.DataRow);
dt.Columns.Add("Series0");
dt.Columns.Add("Series1");
dt.Columns.Add("Series2");
dr = dt.NewRow();
dr[0] = 2;
dr[1] = 1;
dr[2] = 5;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 4;
dr[1] = 2;
dr[2] = 5;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 3;
dr[1] = 4;
dr[2] = 5;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = 3;
dr[1] = 4;
dr[2] = 5;

FpSpread1.DataSource = dt;
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 = "Series0";
this.ListBox1.DataBind();
VB
Copy Code
Dim dt As New System.Data.DataTable("Test")
Dim dr As System.Data.DataRow
dt.Columns.Add("Series0")
dt.Columns.Add("Series1")
dt.Columns.Add("Series2")
dr = dt.NewRow()
dr(0) = 2
dr(1) = 1
dr(2) = 5
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(0) = 4
dr(1) = 2
dr(2) = 5
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(0) = 3
dr(1) = 4
dr(2) = 5
dt.Rows.Add(dr)
dr = dt.NewRow()
dr(0) = 3
dr(1) = 4
dr(2) = 5
FpSpread1.DataSource = dt
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 = "Series0"
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
See Also