New to Telerik UI for WinForms? Download free 30-day trial

Binding to Array and ArrayList

The examples below demonstrate binding to a generic list, an arrays of custom objects, and an ArrayList of custom objects. This collections have limitations when used as a data source in which case a BindingList must be used. 

Note: BindingList is the preferred collection since its changes are automatically reflected on the data-bound control.

Binding to a Array List

The example below creates an ArrayList of generic objects initialized with five values and assigned as a DataSource to the RadGridView.

WinForms RadGridView Binding to a Array List

public class ValueType<T>
{
    T item;
    public ValueType() { }
    public ValueType(T item)
    {
        this.item = item;
    }
    public T ItemProperty
    {
        get { return this.item; }
        set { this.item = value; }
    }
}
ArrayList list = new ArrayList();
for (int i = 0; i < 5; i++)
{
    list.Add(new ValueType<string>("string " + (i + 1).ToString()));
}
this.radGridView1.DataSource = list;

Binding to an Array of Objects

Arrays of objects containing bindable types can be bound to RadGridView by assigning the array to the DataSource property of the grid. 

The example below defines a "MyObject" class containing one integer and one string property. The snippet of code at end of the example creates an array of MyObject, initializes the array with two objects and assigns the array to the DataSource. The MyObject class would typically be placed in its own separate class file and the array creation, initialization and assignment code might be placed in a form's Load event handler.

WinForms RadGridView Binding to an Array of Objects

public class MyObject
{
    public MyObject(int myInt, string myString)
    {
        _myInt = myInt;
        _myString = myString;
    }
    private int _myInt;
    public int MyInt
    {
        get { return _myInt; }
        set { _myInt = value; }
    }
    private string _myString;
    public string MyString
    {
        get { return _myString; }
        set { _myString = value; }
    }
}
MyObject[] myArray = new MyObject[2] { new MyObject(1, "object one"), new MyObject(2, "object two") };
radGridView1.DataSource = myArray;