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

Binding to BindingList

BindingList is a generic list type that has additional binding support. While you can bind to a generic list, BindingList provides additional control over list items, i.e. if they can be edited, removed or added. BindingList also surfaces events that notify when the list has been changed. The example below creates a list of MyObject, initializes the list and assigns the list to the grid DataSource property. The example also uses a ListChangedEventHandler that reports the type of change that occurred, the new index of the item and the content of the item.

WinForms RadGridView Binding to BindingList

public class MyObject
{
    public 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; }
    }
}
private BindingList<MyObject> myList;
private void Form1_Load(object sender, EventArgs e)
{
    myList = new BindingList<MyObject>();
    myList.Add(new MyObject(1, "Outdoor"));
    myList.Add(new MyObject(2, "Hardware"));
    myList.Add(new MyObject(3, "Tools"));
    myList.Add(new MyObject(4, "Books"));
    myList.Add(new MyObject(5, "Appliances"));
    myList.RaiseListChangedEvents = true;
    myList.ListChanged += new ListChangedEventHandler(myList_ListChanged);
    radGridView1.DataSource = myList;
}
void myList_ListChanged(object sender, ListChangedEventArgs e)
{
    MessageBox.Show(e.ListChangedType.ToString() + ": at index: " + e.NewIndex.ToString() + ", \"" + myList[e.NewIndex].MyString + "\"");
}
private void radButton1_Click(object sender, EventArgs e)
{
    myList.Add(new MyObject(6, "Plants"));
}