In grid design we initially wanted to separate the data layer from the presentation layer. The data layer is a set of classes that implement application business logic, e.g. classes describing products and prices, various merchandise, etc. Objects of these classes may be directly added to one or several grids. Such objects may be presented in these grids in different ways with different formats, hierarchy levels, sorting, grouping and filtering.

Please see below an example that demonstrates this separation principle. Data is presented by an object of Product class that is a part of the event-driven model. This means that the Product class implements INotifyPropertyChanged interface. Please note that this object can be located in an assembly that doesn’t have references to Dapfor libraries.

This object can be directly added to multiple grids and can notify them about price changes via INotifyPropertyChanged.

 Copy imageCopy
  //Some data object
public class Product : INotifyPropertyChanged
{
    //Some fields
    private double price;
    private DateTime maturity;

    public double Price
    {
        get { return price; }
        set 
        {
            price = value;
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Price"));
            }
        }
    }

    public DateTime Maturity
    {
        get { return maturity; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}


  //Initializing
 public void PopulateGrids()
{
    //Initialize the first grid
    Grid grid1 = new Grid();
    grid1.Headers.Add(new Header());
    grid1.Headers[0].Add(new Column("Price"));
    grid1.Headers[0]["Price"].Format = new DoubleFormat(3, false, false);

    //Initialize the second grid
    Grid grid2 = new Grid();
    grid2.Headers.Add(new Header());
    grid2.Headers[0].Add(new Column("Price"));
    grid2.Headers[0]["Price"].Format = new DoubleFormat(2, true, false);

    //Create a product object and add it to both grids
    Product product = new Product();

    grid1.Rows.Add(product);
    grid2.Rows.Add(product);

    //Sort data in the second grid
    grid2.Headers[0]["Price"].SortDirection = SortDirection.Ascending;

    //Update the product's price. Both grids will refresh cells automatically and will sort rows if needed.
    //There are no necessity to know the references to those grids, the data object will be displayed correctly
    product.Price = 45.45;
}

Back to .Net Grid HowTo topics