Currency viewer

The grid provides broad capabilities for working with data of any type, including data of arbitrary classes or data implementing IEnumerable or IDictionary interface. Besides that, the programmer has broad capabilities of defining custom data types.

The Сurrency viewer example shows how to create dynamic data types with arbitrary number of fields.

As it has already been mentioned, the grid can work with various data types. For this purpose the grid uses IDataAccessor that normalizes data presentation for the grid and transmits notifications from objects implementing INotifyPropertyChanged to the it. The grid never works with data directly, but only via IDataAccessor. In addition to many implementations of this interface within the package, a programmer may create custom implementations of this interface. IDataAccessor that is available via Row.DataAccessor is created for each data object when data is added to the grid or upon binding to a data source.

The objective of the currency viewer is to display exchange rates for most popular currencies, and the list of these currencies should be easily customizable. Therefore, it is not possible to use regular classes with properties returning rates, since when the number of currencies changes it is necessary to add new properties or modify existing properties in these classes. Use of data objects based on IEnumerable or IDictionary doesn’t provide sufficient flexibility as it doesn’t support event-driven mode (INotitifyPropertyChanged implementation). It means that upon each data modification it becomes necessary to search for grid cells that show requested values and to redraw these cells. With such approach the code will be bulky and fairly complicated.

In this case implementation of IDataAccessor interface is the best solution enabling data objects to dynamically establish the number of existing currencies, send requested currency exchange rate values to the grid and be a part of an event-driven model sending notifications to the grid upon change of rates.

public class CurrencyRow : IDataAccessor
{
    public CurrencyRow(CurrencyType currency)
    {
    }

    ...
}

public void PopulateGrid(Grid grid)
{
    //Creare all available currency pairs
    BindingList<CurrencyRow> dataSource = new BindingList<CurrencyRow>();
    foreach (CurrencyType currency in Enum.GetValues(typeof(CurrencyType)))
    {
        dataSource.Add(new CurrencyRow(currency));
    }

    //Attach grid to data source
    grid.DataSource = dataSource;
}