# Binding a Cell Range in Spread as a Data Source to an External Control

Learn how to bind a cell range in Spread as a data source for an external control, such as a DataGrid control. Create custom formatters and mappers to enhance data presentation and manipulation.

## Content

You can bind a range of cells in Spread as a data source for an external control such as a **DataGrid** control. The following diagram shows the objects involved.
![Binding Spread Cell Range as a Data Source to an External Control](https://cdn.mescius.io/document-site-files/images/2238e618-a1fb-45a6-9ce5-9a4157bd2931/images/diagram-extcontroltospreadrange.png)
These are the objects involved:

* [SpreadDataView](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.SpreadDataView.html)
* [SpreadDataRowView](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.SpreadDataView.html)
* [ISpreadDataViewDataFormatter](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.ISpreadDataViewDataFormatter.html)
* [ISpreadDataViewMapper](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.ISpreadDataViewMapper.html)
* [DefaultSpreadDataViewDataFormatter](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.DefaultSpreadDataViewDataFormatter.html)
* [DefaultSpreadDataViewMapper](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.DefaultSpreadDataViewMapper.html)

## Creating a Custom Formatter

You can create a custom formatter class by inheriting from [ISpreadDataViewDataFormatter](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.ISpreadDataViewDataFormatter.html).

## Using Code

1. Create the class.
2. Assign the class to the data view.

## Example

This example code creates a custom class.

```csharp
public class MySpreadDataViewDataFormatter : ISpreadDataViewDataFormatter
{
private SpreadDataColumn column;
private SheetView sheetView;
public SheetView SheetView;
{
get { return sheetView; }
set { sheetView = value; }
}
public MySpreadDataViewDataFormatter(SpreadDataColumn ownerColumn,SheetView sheetView)
{
if (ownerColumn == null)
{
throw new ArgumentNullException("ownerColumn");
}
column = ownerColumn;
this.SheetView = sheetView;
}
public object GetCellValue(Cell cell)
{
object ret = null;
try
{
ret = this.SheetView.GetValue(cell.Row.Index, cell.Column.Index);
ret += ": Customized format";
}
catch
{
ret = " No value";
}
return ret;
}
public void SetCellValue(Cell cell, object value)
{
this.SheetView.SetValue(cell.Row.Index, cell.Column.Index, value + ": Customized format");
}
}
// Assign new formatter
dataSet = BuildDataSet(5,5);
this.spreadDataBindingAdapter1.Spread = this.fpSpread1;
this.spreadDataBindingAdapter1.SheetName = this.fpSpread1.ActiveSheet.SheetName;
this.spreadDataBindingAdapter1.DataSource = dataSet.Tables[0];
spreadDataBindingAdapter1.MapperInfo = new MapperInfo(1, 2, 3, 4);
MySpreadDataViewDataFormatter testFormatter = new MySpreadDataViewDataFormatter
(this.spreadDataBindingAdapter1.SpreadDataView.Columns[2], fpSpread1.ActiveSheet);
this.spreadDataBindingAdapter1.SpreadDataView.Columns[2].Formatter = testFormatter;
this.spreadDataBindingAdapter1.FillSpreadDataByDataSource();
```

## Creating a Custom Mapper

You can create a custom mapper class by inheriting from [ISpreadDataViewMapper](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Data.ISpreadDataViewMapper.html).

## Using Code

1. Create the class.
2. Assign the class to the data view.

## Example

This example code creates a custom class.

```csharp
public class MySpreadDataViewMapper : ISpreadDataViewMapper
{
...
}
//Assign customized Mapper for SpreadDataView
dataSet = BuildDataSet(5,5);
this.spreadDataBindingAdapter1.Spread = this.fpSpread1;
this.spreadDataBindingAdapter1.SheetName = this.fpSpread1.ActiveSheet.SheetName;
this.spreadDataBindingAdapter1.DataSource = dataSet.Tables[0];
MySpreadDataViewMapper testMapper = new MySpreadDataViewMapper ();
this.spreadDataBindingAdapter1.SpreadDataView.Mapper = testMapper;
spreadDataBindingAdapter1.MapperInfo = new MapperInfo(1, 2, 3, 4);
this.spreadDataBindingAdapter1.FillSpreadDataByDataSource();
```

```vbnet
Public Class MySpreadDataViewMapper
Implements FarPoint.Win.Spread.Data.ISpreadDataViewMapper
...
End Class
dataSet = BuildDataSet(5, 5)
Me.spreadDataBindingAdapter1.Spread = Me.fpSpread1
Me.spreadDataBindingAdapter1.SheetName = Me.fpSpread1.ActiveSheet.SheetName
Me.spreadDataBindingAdapter1.DataSource = dataSet.Tables(0)
Dim testMapper As New MySpreadDataViewMapper()
Me.spreadDataBindingAdapter1.SpreadDataView.Mapper = testMapper
spreadDataBindingAdapter1.MapperInfo = New MapperInfo(1, 2, 3, 4)
Me.spreadDataBindingAdapter1.FillSpreadDataByDataSource()
```

## See Also

[Binding Spread to an External Data Set](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-data-binding/spwin-databind/spwin-databind-howto)
[Binding a Cell Range in Spread to an External Data Source](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-data-binding/spwin-databind/spwin-databind-range)
[Binding a Combo Box to a DataReader](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-data-binding/spwin-databind/spwin-databind-comboreader)