We have already mentioned that the grid supports various data types from object[] to objects of arbitrary classes. To achieve higher performance it is better to add data to the end of an unsorted grid. Please see below an example that demonstrates filling of the grid with data.

C# Copy imageCopy
//Some business 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;
}


public void PopulateGrid(Grid grid)
{
    Product product1 = new Product();
    Product product2 = new Product();
    Product product3 = new Product();

    //Add data objects on the top hierarchical level. 
    Row row1 = grid.Rows.Add(product1);
    Row row2 = grid.Rows.Add(product2);

    //Add a data objects as a child of row2. 
    Row row3 = row2.Add(product3);

    //Add a collection of values to the grid. 
    //Because of this collection implements IList, it will be implicitly wrapped by the ListDataAccessor
    grid.Rows.Add(new double[] { 123, 12, 45 });

    //Add a collection of objects
    grid.Rows.Add(new object[] { 12.36, 12, "some string" });

    //Add a collection of strings
    grid.Rows.Add(new string[] { "string 1", "string 2", "string 3" });

    //Add a dictionary of values to the grid. 
    Hashtable hashTable = new Hashtable();
    hashTable.Add("Price", 10.23);
    hashTable.Add("Quantity", 645);
    grid.Rows.Add(hashTable);
}

Back to .Net Grid HowTo topics