Overview

A data source is an abstract interface that defines the origin of a data flow.

A data source can be implemented as a physical array, a custom class in your application, or even a ReoGrid worksheet itself.

By implementing the data source interface, you can flexibly configure data flows and supply data to worksheets in various ways.

You can also apply filters to a data source to process the data before passing it to the next input. Using data sources allows you to flexibly manage large-scale reporting systems.

Interface Definition

A data source is defined through the IDataSource interface. It serves as an abstract interface for providing data to a worksheet, with no restrictions on the specific type or kind of data.

A data source provides the Enumerator interface of IDataRecord and acts as a collection containing multiple rows of records. IDataRecord represents a single row of data.

public interface IDataSource<out T> : IEnumerable<T> where T : IDataRecord
{
  event EventHandler<DataSourceChangedEventArgs> OnInputDataChanged;
}

Synchronization on Data Source Change

By implementing the OnInputDataChanged event, you can notify downstream inputs when the data changes, allowing changes to be reflected in the worksheet in real time.

Setting a Data Source on a Worksheet

You can add a data source to a worksheet using the Worksheet.AddDataSource method.

sheet.AddDataSource("A1:G30", dataSource);

ReoGrid supports adding multiple data sources to a single worksheet.

Implementing a Custom Data Source

By implementing the IDataSource interface, you can define your own custom data source.

public class MyDataSource : IDataSource<IDataRecord>
{
  private object[,] data;
  ...
}

By implementing IDataSource this way, you can define a data source tailored to your application’s unique data structure or retrieval method. For example, you can build a mechanism to reflect data retrieved from a database, a web API, or another external system directly into a worksheet.

As an implementation example, you can create a MyDataRecord class that implements IDataRecord and holds the necessary data fields, allowing you to support any data structure.

public class MyDataRecord : IDataRecord
{
  public object[] Values { get; }

  public MyDataRecord(params object[] values)
  {
    Values = values;
  }

  public object GetData(int columnIndex) => Values[columnIndex];
}

This enables you to configure data display and update processing in ReoGrid in a flexible and extensible manner.

Built-in Data Sources in ReoGrid

ReoGrid currently provides the following two built-in data sources:

  • ArrayDataSource: A simple data source that holds data in a standard object[,] format.
  • Column-based Data Source: A flexible format that manages data using column IDs instead of column indices.

Column-based Data Source :nri

In a column-based data source, you define columns in advance and use their corresponding IDs to register and manipulate data. This is implemented using the ColumnBasedDataSource class.

// Create data source
var ds = new ColumnBasedDataSource();

// Define columns
ds.Columns.AddRange("id", "tsuka", "kamoku", "baibai", "zengaku", "hiduke", "biko");

// Store data
for (int r = 0; r < 30; r++)
{
  var record = ds.Records.AppendNew();

  // ID
  record["id"] = 15001;
  // Currency
  record["tsuka"] = "USD";
  // Subject
  record["kamoku"] = "015";
  // Trade type
  record["baibai"] = "Sell";
  // Total amount
  record["zengaku"] = 105800;
  // Date
  record["hiduke"] = new DateTime(2020, 11, 1);
  // Remarks
  record["biko"] = "...";
}

// Add data source to worksheet; A1:G30 is the target range address
sheet.AddDataSource("A1:G30", ds);
Was this article helpful?