Spread ASP.NET 6.0 Product Documentation
Binding to a Data Source
Send Feedback
Spread ASP.NET 6.0 Product Documentation > Developer's Guide > Managing Data Binding > Binding to a Data Source

Glossary Item Box

You can bind the component to a data set. When you bind the component using the default settings, data from the data set is read into the columns and rows of the sheet to which you bind the data. Columns are associated with fields, and rows represent each record in the data set.

The following instructions provide the code necessary to bind the FpSpread component to a data set. For a detailed walk-through of binding to a database see, Tutorial: Binding to a Corporate Database.

The basic procedure is to bind data either to the sheet directly or to a data model that the sheet uses. This means either setting the DataSource member in the SheetView class or using the DefaultSheetDataModel class to construct a data model and then in the SheetView class set the DataModel property to the newly created data model.

Sometimes, you may want to specify a particular data table within a data source that has multiple tables. Use the SheetView class, DataMember property to specify the particular data member.

You can perform more specific manipulations once the data is bound. You can use the DataField property in the Column class to bind a column to a particular data field. For instance, if you want the bound column 3 to be displayed first on the spreadsheet, or if you want to hide a column of data, or if you simply want to display the bound columns in general in a different order than they exist in the data set, you can use the DataField property in the particular column to achieve this.

You might want to specify the key number as opposed to the row number; if so, use the DataKeyField property of the sheet.

You can use the SQLDataAdapter instead of the OLEDB adapter when binding to a data set. Use the SQLDataAdapter to set up your data set and bind it to the spreadsheet.

In bound mode, the data model wraps the supplied data source and if needed can supply additional data and interactivity not available from the data source, for example cell formulas and unbound rows and columns. In a few cases, you may need to create your own custom data model for performance reasons. For more information about developing custom data models, refer to Using Sheet Models.

How you handle state management while bound to a database or working with large data sets can affect your application’s performance. You need to set up state management to optimize performance and account for other factors. For more information, refer to Maintaining State.

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 Code

  1. Create a data model and set it equal to a DefaultSheetDataModel object, specifying a data set for the new model’s dataSource parameter.

    Other parameter options are available for the DefaultSheetDataModel constructor. Refer to the Assembly Reference for complete information.

  2. Set the SheetView object’s DataModel property equal to the new data model.

Example

This example code binds the FpSpread component to a data set named dataSet1.

C# Copy Code
// Create a data model using a data set.
FarPoint.Web.Spread.Model.DefaultSheetDataModel model =     new FarPoint.Web.Spread.Model.DefaultSheetDataModel(dataSet1);
FpSpread1.Sheets[0].DataModel = model;
VB Copy Code
' Create a data model using a data set.
Dim model   As New FarPoint.Web.Spread.Model.DefaultSheetDataModel(dataSet1) FpSpread1.Sheets(0).DataModel = model

Using a Shortcut

Set the FpSpread DataSource property.

Example

This example code uses the FpSpread DataSource property to bind to a data set.

C# Copy Code
FpSpread1.DataSource = ds;
VB Copy Code
FpSpread1.DataSource = ds

Using Code

If your data set is not getting 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.

Example

This example code sets the DataBind method.

C# Copy Code
if !(Page.IsPostBack)
{
     FpSpread1.DataBind();
}
VB Copy Code
If Not Page.IsPostBack Then
     FpSpread1.DataBind()
End If
© 2002-2012 GrapeCity, Inc. All Rights Reserved.