Overview
A data source is an abstract interface that defines the origin of data flow.
A data source can be implemented as a physical array, a custom class in your application, or even as a ReoGrid worksheet itself.
By implementing the data source interface, you can flexibly configure data flows and input data into worksheets in various ways.
You can also apply filters to a data source to process the data and pass it to the next input. Utilizing data sources allows you to flexibly handle large-scale reporting systems.
Interface Definition
A data source is defined through the IDataSource
interface.
A data source serves as an abstract interface for providing data to a worksheet, without restrictions on the specific type or kind of data.
A data source provides the Enumerator
interface of IDataRecord
and functions 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
{
// TODO: add event arguments to determine which data is changed
event EventHandler OnInputDataChanged;
}
Synchronization on Data Source Change
By implementing the OnInputDataChanged
event, you can notify subsequent inputs when the input data changes, allowing changes to be reflected in the worksheet in real time.
Setting a Data Source to 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.
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
in 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 obtained from a database, web API, or other external systems 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 this[int index] => Values[index];
}
This enables you to configure data display and update processing in ReoGrid in a flexible and extensible manner.
Built-in Data Sources in ReoGrid
Currently, ReoGrid 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 handles data by column ID instead of column index.
Column-based Data Source
In a column-based data source, you define columns in advance and use 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);